简体   繁体   English

将Java Spring Rest Controller中发布的JSON映射到POJO

[英]Map JSON POSTed in Java Spring Rest Controller to POJO

I am sending the following JSON object to my Java Spring application 我将以下JSON对象发送到我的Java Spring应用程序

{
    "ewsUrl":"https://dummy.url.com",
    "ewsIdentityToken":"12345",
    "itemIds":["itemids"],
    "entityId":null,
    "documentType":"Dummy",
    "documentStatus":"Record",
    "filename":"message.eml",
    "metadata":{"title":"message"}
}

I have defined an object public class RequestObject and in my controller I have 我定义了一个对象public class RequestObject ,在我的控制器中

  public RequestObject 
    testMyStuff(@CurrentUser User currentUser, 
    @RequestBody RequestObject myDummyObject) throws Exception {
       return myDummyObject
    }

My application returns the error Could not read document: Root name 'ewsUrl' does not match expected ('RequestObject') for type...etc 我的应用程序返回错误Could not read document: Root name 'ewsUrl' does not match expected ('RequestObject') for type...etc

However if I send the JSON formatted like this it successfully maps the object: 但是,如果我发送像这样格式化的JSON,它将成功映射对象:

{ "RequestObject":
    {
        "ewsUrl":"https://dummy.url.com",
        "ewsIdentityToken":"12345",
        "itemIds":["itemids"],
        "entityId":null,
        "documentType":"Dummy",
        "documentStatus":"Record",
        "filename":"message.eml",
        "metadata":{"title":"message"}
    }
}

I do not want to name the object in my JSON, I want to send as described in the first example. 我不想在JSON中命名该对象,而是要按照第一个示例中的说明进行发送。 How do I achieve this? 我该如何实现?

UPDATE: 更新:

RequestObject.java RequestObject.java

public class RequestObject {

    public String ewsUrl;
    public String ewsIdentityToken;
    public String[] itemIds;
    public String entityId;
    public String documentType;
    public String documentStatus;
    public String filename;
    public Metadata metadata;

    public RequestObject() {
    }

    public static class Metadata {
        public String title;
    }
}

UPDATE2: UPDATE2:

The way it is described in this example suggests that the object does not need to be named in the JSON data in the POST request. 此示例中描述的方式表明,无需在POST请求的JSON数据中命名该对象。 I think I am emulating this example, but I'm getting different results. 我想我在模仿这个例子,但结果却有所不同。 Is there a configuration for Jackson/Spring that I am missing? 我缺少Jackson / Spring的配置吗?

Update 3: 更新3:

The complete error message is: 完整的错误消息是:

Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: 
Could not read document: 
Root name 'ewsUrl' does not match expected ('RequestObject') for type 
[simple type, class uk.co.test.RequestObject] at [Source: 
java.io.PushbackInputStream@7e223182; line: 2, column: 9]; 
nested exception is com.fasterxml.jackson.databind.JsonMappingException: 
Root name 'ewsUrl' does not match expected ('RequestObject') for type 
[simple type, class uk.co.test.RequestObject]

There's some configuration settings that look like they can be defined for the ObjectMapper that controls the behaviour of the root element: 看起来可以为控制根元素行为的ObjectMapper定义一些配置设置:

UNWRAP_ROOT_MODULE is disabled by default according to the docs so not sure why you're seeing the behaviour you are. 默认情况下,根据文档,UNWRAP_ROOT_MODULE是禁用的,因此不确定您为什么会看到自己的行为。

Config example for spring is available at http://docs.spring.io/spring-framework/docs/3.2.3.RELEASE/javadoc-api/org/springframework/http/converter/json/JacksonObjectMapperFactoryBean.html 可在http://docs.spring.io/spring-framework/docs/3.2.3.RELEASE/javadoc-api/org/springframework/http/converter/json/JacksonObjectMapperFactoryBean.html上获取spring的配置示例。

Just use JSONArray instead of JSONObject 只需使用JSONArray而不是JSONObject

Update 更新资料

You can get your Json Object via JSONArray.getJSONObject() 您可以通过JSONArray.getJSONObject()获取Json对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM