Spring MVC @RequestBody接收具有非原始属性的对象包装

我按如下方式创建JSON:

    var manager = {

username: "admin",

password: "admin"

};

var userToSubscribe = {

username: "newuser",

password: "newpassword",

email: "user@1and1.es"

};

var openid = "myopenid";

var subscription = {

manager: manager,

userToSubscribe : userToSubscribe,

openid : openid

};

$.ajax({

url: '/myapp/rest/subscribeUser.json',

type: 'POST',

dataType: 'json',

contentType: 'application/json',

mimeType: 'application/json',

data: JSON.stringify({subscription : subscription})

});

这是发送的JSON:

{"subscription":{"manager":{"username":"admin","password":"admin"},"userToSubscribe":{"username":"newuser","password":"newpassword","email":"user@1and1.es"},"openid":"myopenid"}}

我想将此JSON映射到Wrapper类。这是包装器:

private class Subscription{

private User manager;

private User userToSubscribe;

private String openid;

public User getManager() {

return manager;

}

public void setManager(User manager) {

this.manager = manager;

}

public User getUserToSubscribe() {

return userToSubscribe;

}

public void setUserToSubscribe(User userToSubscribe) {

this.userToSubscribe = userToSubscribe;

}

public String getOpenid() {

return openid;

}

public void setOpenid(String openid) {

this.openid = openid;

}

}

pom.xml中的杰克逊依赖关系(我正在使用spring 3.1.0.RELEASE):

    <dependency>

<groupId>org.codehaus.jackson</groupId>

<artifactId>jackson-mapper-asl</artifactId>

<version>1.9.10</version>

</dependency>

rest-servlet.xml中的映射

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">

<property name="messageConverters">

<list>

<ref bean="jsonConverter" />

</list>

</property>

</bean>

<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">

<property name="supportedMediaTypes" value="application/json" />

</bean>

和控制器方法的标头:

public @ResponseBody SimpleMessage subscribeUser(@RequestBody Subscription subscription)

由于POST,我收到400错误请求请求错误。是否可以执行此操作,或者我是否需要使用@RequestBodyString进行操作或@RequestBody

Map<String,Object>自己解码JSON?

谢谢!

回答:

我要回答我自己的问题。首先要特别感谢Sotirios Delimanolis,因为他给了我钥匙以调查发生了什么。

如您所知,我从视图创建以下json:

{"manager":{"username":"admin","password":"admin"},"userToSubscribe":{"username":"newuser","password":"newpassword","email":"user@1and1.es"},"openid":"https://myopenid..."}

我做了一点修改,因为我意识到创建对象订阅和var订阅不是必需的。如果您像这样构建JSON,它将可以完美地工作:

var manager = {

username: "admin",

password: "admin"

};

var userToSubscribe = {

username: "newuser",

password: "newpassword",

email: "user@1and1.es"

};

var openid = "https://myopenid...";

$.ajax({

url: '/dp/rest/isUserSuscribed.json',

type: 'POST',

dataType: 'json',

contentType: 'application/json',

mimeType: 'application/json',

data: JSON.stringify({manager : manager, userToSubscribe : userToSubscribe, openid : openid})

});

控制器收到以下json:

@RequestMapping(method=RequestMethod.POST, value="/isUserSuscribed.json")

public @ResponseBody ResponseMessageElement<Boolean> isUserSuscribed(@RequestBody SubscriptionWrapper subscription){

还有SubscriptionWrapper …

private static class SubscriptionWrapper {

BasicUser manager;

BasicUser userToSubscribe;

String openid;

public BasicUser getManager() {

return manager;

}

public void setManager(BasicUser manager) {

this.manager = manager;

}

public BasicUser getUserToSubscribe() {

return userToSubscribe;

}

public void setUserToSubscribe(BasicUser userToSubscribe) {

this.userToSubscribe = userToSubscribe;

}

public String getOpenid() {

return openid;

}

public void setOpenid(String openid) {

this.openid = openid;

}

}

那是什么问题 我收到了一个不正确的Request

400错误…我按照Sotirios的建议调试了MappingJackson2HttpMessageConverter,但发生了异常(没有合适的构造函数)。无论此类不是静态的,Jackson

Mapping都无法构建内部类。将SubscriptionWrapper设置为静态是解决我的问题的方法。

您还可以检查以下答案: http://stackoverflow.com/questions/8526333/jackson-error-no-

suitable-constructor

http://stackoverflow.com/questions/12139380/how-to-convert-json-into-pojo-in-

java-using-jackson

如果您有反序列化的问题,请检查以下内容: http://stackoverflow.com/questions/17400850/is-jackson-

really-unable-to-deserialize-json-into-a-generic-type

感谢所有的答复。

以上是 Spring MVC @RequestBody接收具有非原始属性的对象包装 的全部内容, 来源链接: utcz.com/qa/410212.html

回到顶部