简体   繁体   中英

How to deserialize a HttpEntity containing Array Lists from Rest API call to a Java Object?

I am trying to deserialize ResponseEntity<> returned by a Rest API call to Java Objects and had been unsuccessful. Following is the POJO


    public class Application {
           private String name;
           private List<String> location;
    } 

Here is the Rest API call and deserializing

public List<Application> getApps(){
       List<Application> = new ArrayList<>();
       ResponseEntity<String> returnObject = oauthRestTemplate.exchange(url, HttpMethod.POST, httpEntity, 
           String.class);
       ObjectMapper objectMapper = new ObjectMapper();
       Response response = objectMapper.readValue(returnObject.getBody(),
           Response.class);
       apps = objectMapper.convertValue(
                    response.getItems(),
                    new TypeReference<List<Application>>() {
                    }
            );
       return apps;
}

Response Java Object is


    public class Response {
           private String code;
           private String message;
           private String Status;
           private List<T> items;
    }

Contents of return response (returnObject.getBody()) is


    <Response>
        <code>200</code>
        <message></message>
        <status>SUCCESSFUL</status>
        <items>
            <items>
                <name>App1</name>
                <location>
                    <location>location1</location>
                </location>
            </items>
            <items>
                <name>App2</name>
                <location>
                    <location>location1</location>
                </location>
            </items>
        </items>
    </Response>

It is difficult to understand why additional tags for List is being added to the JSON string. The deserializing is failing when it encounters location tag within location. Following are the error logs


    com.fasterxml.jackson.core.JsonParseException: Unexpected character ('<' (code 60)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
     at [Source: (String)"<PagedResponse><code>200</code><message/><status>SUCCESSFUL</status><items><items><name>App1</name><location>" line: 1, column: 2]

Can someone help me understand what am I doing wrong here?

UPDATE Modified the "ObjectMapper" to "XmlMapper" and received the below error.


    java.lang.IllegalArgumentException: Cannot deserialize instance of `java.util.ArrayList` out of START_OBJECT token
     at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: java.util.ArrayList[0]->com.example.dto.Application["location"])
            at com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:3750)
            at com.fasterxml.jackson.databind.ObjectMapper.convertValue(ObjectMapper.java:3678)

It looks like you are using a JSON parser to parse XML. It fails at the beginning: line: 1, column: 2

If your response is in XML format user XMLMapper. Here is the reference https://www.baeldung.com/jackson-xml-serialization-and-deserialization

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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