Spring MVC @RequestBody接收具有非基本属性的Object包装器 [英] Spring MVC @RequestBody receive an Object wrapper with non-primitive attributes

查看:131
本文介绍了Spring MVC @RequestBody接收具有非基本属性的Object包装器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按如下方式创建JSON:

I create the JSON as follows:

    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类。这是包装器:

And I would like to map this JSON to a Wrapper Class. This is the 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中的jackson依赖项(我' m使用spring 3.1.0.RELEASE):

The jackson dependency in the pom.xml (I'm using 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中的映射

The mapping in 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错误的请求错误。是否可以这样做或者我需要使用 @RequestBody 字符串或 @RequestBody Map< String,Object> 并自己解码JSON?

As a result of the POST I receive a 400 Incorrect request error. Is it possible to do this or do i need to do it with @RequestBody String or @RequestBody Map<String,Object> and decode the JSON myself?

谢谢!

推荐答案

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

I'm going to answer my own question. First of all special thanks to Sotirios Delimanolis because he gave me the key in order to investigate what it was happening.

如你所知,我从视图中创建了以下json :

As you know, I create the following json from the view:

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

我改变了一点因为我意识到创建对象Subscription和var Subscription不是必需的。如果你像这样构建JSON,它将完美地工作:

I changed it a little bit because I realised that is not necessary to create a object Subscription and a var Subscription. If you build the JSON like this, it will work perfectly:

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:

The controller receives this json:

@RequestMapping(method=RequestMethod.POST, value="/isUserSuscribed.json")
public @ResponseBody ResponseMessageElement<Boolean> isUserSuscribed(@RequestBody SubscriptionWrapper subscription){

以及SubscriptionWrapper ...

And the 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;
    }
}

那么......有什么问题?我收到了错误的请求400错误...我调试了MappingJackson2HttpMessageConverter作为Sotirios建议并且有一个异常(没有合适的构造函数)。 Jackson Mapping无法构建内部类,无论此类是否不是静态的。将SubscriptionWrapper设置为static是解决我的问题的方法。

So... What is the problem? I was receiving an Incorrect Request 400 error... I debugged the MappingJackson2HttpMessageConverter as Sotirios suggested and there was an exception (No suitable constructor). Jackson Mapping is not able to build an inner class whether this class is not static. Setting SubscriptionWrapper to static was the solution to my problem.

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

You can also check these answers: 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

And if you have problems to deserialize, check this: http://stackoverflow.com/questions/17400850/is-jackson-really-unable-to-deserialize-json-into-a-generic-type

感谢所有回复。

这篇关于Spring MVC @RequestBody接收具有非基本属性的Object包装器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆