Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
383 views
in Technique[技术] by (71.8m points)

json - 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)
question from:https://stackoverflow.com/questions/66055867/how-to-deserialize-a-httpentity-containing-array-lists-from-rest-api-call-to-a-j

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...