我正在尝试调用外部RESTful Web服务,并且需要解析嵌套JSON附带的响应。我试图解析以下内容:
{“list”:[{“list_id”:“abcdef”,“list_name”:“testing123”},{“list_id”:“xyz”,“list_name”:“testing456”}}}
我的POJO课程如下:
@JsonIgnoreProperties(ignoreUnknown = true)
public class ListGetResponse {
private String list = "";
private SMSList lists;
public String getList() {
return list;
}
public void setList(String list) {
this.list = list;
}
public SMSList getLists() {
return lists;
}
public void setLists(SMSList lists) {
this.lists = lists;
}
@Override
public String toString() {
return "SMSListGetResponse [list=" + list + "]";
}
@JsonIgnoreProperties(ignoreUnknown = true)
public class SMSList {
private String list = "";
private String list_id = "";
private String list_name = "";
public String getList() {
return list;
}
public void setList(String list) {
this.list = list;
}
public String getList_id() {
return list_id;
}
public void setList_id(String list_id) {
this.list_id = list_id;
}
public String getList_name() {
return list_name;
}
public void setList_name(String list_name) {
this.list_name = list_name;
}
@Override
public String toString() {
return "SMSList [list_id=" + list_id + ", list_name=" + list_name
+ "]";
}
}
我通过这种方法调用此函数。
public void get()
{
RestTemplate restTemplate = SMSRestFactory.getRestTemplate();
ResponseEntity<ListGetResponse> responseEntity = restTemplate.getForEntity(SMS_LIST_GET, ListGetResponse.class);
ListGetResponse body = responseEntity.getBody();
System.out.println(responseEntity.getBody());
System.out.println(body);
}
调用get()时;我收到以下StackTrace
Exception in thread "main" org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Can not deserialize instance of java.lang.String out of START_ARRAY token
at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@165ae07; line: 1, column: 2] (through reference chain: com.em.ustms.sms.list.model.ListGetResponse["list"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_ARRAY token
at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@165ae07; line: 1, column: 2] (through reference chain: com.em.ustms.sms.list.model.ListGetResponse["list"])
at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.readJavaType(MappingJackson2HttpMessageConverter.java:228)
at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.read(MappingJackson2HttpMessageConverter.java:220)
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:95)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:788)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:773)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:553)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:506)
at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:268)
at com.em.ustms.sms.service.SMSListService.get(SMSListService.java:59)
at com.em.ustms.sms.Application.main(Application.java:12)
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_ARRAY token
at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@165ae07; line: 1, column: 2] (through reference chain: com.em.ustms.sms.list.model.ListGetResponse["list"])
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:164)
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:691)
at com.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:46)
at com.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:11)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:525)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:99)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:242)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:118)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2993)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2158)
at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.readJavaType(MappingJackson2HttpMessageConverter.java:225)
... 9 more
答案 0 :(得分:1)
让它发挥作用。将我的SMSList对象更改为List&lt; SMSList&gt;并将SMSList类更改为静态类
将SMSList更改为List&lt; SMSList&gt;我正在收到以下的堆栈跟踪:
Exception in thread "main" org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: No suitable constructor found for type [simple type, class com.em.ustms.sms.list.model.ListGetResponse$SMSList]: can not instantiate from JSON object (need to add/enable type information?)
at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@1d1ab77; line: 1, column: 11] (through reference chain: com.em.ustms.sms.list.model.ListGetResponse["list"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class com.em.ustms.sms.list.model.ListGetResponse$SMSList]: can not instantiate from JSON object (need to add/enable type information?)
at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@1d1ab77; line: 1, column: 11] (through reference chain: com.em.ustms.sms.list.model.ListGetResponse["list"])
at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.readJavaType(MappingJackson2HttpMessageConverter.java:228)
at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.read(MappingJackson2HttpMessageConverter.java:220)
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:95)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:788)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:773)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:553)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:506)
at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:268)
at com.em.ustms.sms.service.SMSListService.get(SMSListService.java:59)
at com.em.ustms.sms.Application.main(Application.java:12)
Caused by: com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class com.em.ustms.sms.list.model.ListGetResponse$SMSList]: can not instantiate from JSON object (need to add/enable type information?)
at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@1d1ab77; line: 1, column: 11] (through reference chain: com.em.ustms.sms.list.model.ListGetResponse["list"])
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:164)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1078)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:268)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:124)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:227)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:204)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:23)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:525)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:99)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:242)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:118)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2993)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2158)
at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.readJavaType(MappingJackson2HttpMessageConverter.java:225)
... 9 more
通过将SMSClasss更改为静态类来解决此问题:
import java.util.List;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public class ListGetResponse {
private List<SMSList> list;
public List<SMSList> getList() {
return list;
}
public void setList(List<SMSList> list) {
this.list = list;
}
@Override
public String toString() {
return "SMSListGetResponse [list=" + list + "]";
}
@JsonIgnoreProperties(ignoreUnknown = true)
public static class SMSList {
private String list_id = "";
private String list_name = "";
public String getList_id() {
return list_id;
}
public void setList_id(String list_id) {
this.list_id = list_id;
}
public String getList_name() {
return list_name;
}
public void setList_name(String list_name) {
this.list_name = list_name;
}
@Override
public String toString() {
return "SMSList [list_id=" + list_id + ", list_name=" + list_name
+ "]";
}
}
}