Stevo
Stevo

Reputation: 2639

Map JSON POSTed in Java Spring Rest Controller to POJO

I am sending the following JSON object to my Java Spring application

{
    "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 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

However if I send the JSON formatted like this it successfully maps the object:

{ "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. How do I achieve this?

UPDATE:

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:

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. I think I am emulating this example, but I'm getting different results. Is there a configuration for Jackson/Spring that I am missing?

Update 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]

Upvotes: 0

Views: 535

Answers (2)

richhe
richhe

Reputation: 36

There's some configuration settings that look like they can be defined for the ObjectMapper that controls the behaviour of the root element:

UNWRAP_ROOT_MODULE is disabled by default according to the docs so not sure why you're seeing the behaviour you are.

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

Upvotes: 2

cihan adil seven
cihan adil seven

Reputation: 570

Just use JSONArray instead of JSONObject

Update

You can get your Json Object via JSONArray.getJSONObject()

Upvotes: 0

Related Questions