[英]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.我正在尝试反序列化 Rest API 对 Java 对象的调用返回的 ResponseEntity<> 并没有成功。 Following is the POJO
以下是 POJO
public class Application {
private String name;
private List<String> location;
}
Here is the Rest API call and deserializing这是 Rest API 调用和反序列化
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响应 Java Object 是
public class Response {
private String code;
private String message;
private String Status;
private List<T> items;
}
Contents of return response (returnObject.getBody()) is返回响应(returnObject.getBody())的内容是
<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.很难理解为什么要将 List 的附加标签添加到 JSON 字符串中。 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.更新将“ObjectMapper”修改为“XmlMapper”并收到以下错误。
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.看起来您正在使用 JSON 解析器来解析 XML。 It fails at the beginning:
line: 1, column: 2
它在开始时失败:
line: 1, column: 2
If your response is in XML format user XMLMapper.如果您的响应是 XML 格式的用户 XMLMapper。 Here is the reference https://www.baeldung.com/jackson-xml-serialization-and-deserialization
这里是参考https://www.baeldung.com/jackson-xml-serialization-and-deserialization
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.