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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…