gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:03:58 25 4
gpt4 key购买 nike

我按如下方式创建 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 映射到包装器类。这是包装器:

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 中的 jackson 依赖(我使用的是 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>

以及 Controller 方法的头部:

public @ResponseBody SimpleMessage subscribeUser(@RequestBody Subscription subscription)

作为 POST 的结果,我收到了 400 Incorrect 请求错误。是否可以这样做,或者我需要用 @RequestBody 来做吗?字符串或 @RequestBody Map<String,Object>并自己解码 JSON?

谢谢!

最佳答案

查看您的 JSON

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

根元素是 subscription,它是一个 JSON 对象。您的 Subscription 类没有 subscription 字段。所以没有任何东西可以将 subscription 元素映射到,因此它失败并显示 400 Bad Request。

创建一个类SubscriptionWrapper

public class SubscriptionWrapper {
private Subscription subscription;

public Subscription getSubscription() {
return subscription;
}

public void setSubscription(Subscription subscription) {
this.subscription = subscription;
}
}

并更改您的处理程序方法以接受此类型的参数

public @ResponseBody SimpleMessage subscribeUser(@RequestBody SubscriptionWrapper subscriptionWrapper)

您可能需要在 MappingJacksonHttpMessageConverter 中配置 ObjectMapper(仅供引用,您应该使用 MappingJackso2nHttpMessageConverter),以便忽略缺失的属性。

关于java - Spring MVC @RequestBody 接收具有非原始属性的对象包装器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19703540/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com