8

我按如下方式创建 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 中的 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>

以及控制器方法的标头:

public @ResponseBody SimpleMessage subscribeUser(@RequestBody Subscription subscription)

作为 POST 的结果,我收到 400 Incorrect request 错误。是否有可能做到这一点,或者我需要用@RequestBodyString 来做,还是@RequestBody Map<String,Object>自己解码 JSON?

谢谢!

4

3 回答 3

13

查看您的 JSON

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

根元素是subscription,它是一个 JSON 对象。您的Subscription班级没有subscription字段。所以没有任何东西可以映射subscription元素,因此它会失败并出现 400 错误请求。

创建一个类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)

您可能需要配置ObjectMapperin MappingJacksonHttpMessageConverter(仅供参考,您应该使用MappingJackso2nHttpMessageConverter),以便它忽略缺少的属性。

于 2013-10-31T13:32:16.420 回答
5

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

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

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

我对其进行了一些更改,因为我意识到创建对象 Subscription 和 var Subscription 是不必要的。如果你像这样构建 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){

而订阅包装器...

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 错误...我按照 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

感谢所有的答复。

于 2013-11-14T11:22:27.293 回答
2

您无需自己执行此操作。您需要在 pom 中添加此依赖项:

<dependencies>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.8.5</version>
    </dependency>
  </dependencies>

之后 Spring 将为您进行转换。

于 2013-10-31T10:31:27.863 回答