我收到错误:
Exception in thread "main" org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Can not deserialize instance of com.zee.FilterVO out of START_ARRAY token. at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@1b54362; line: 1, column: 1]; nested exception is org.codehaus.jackson.map.JsonMappingException: : Can not deserialize instance of com.zee.FilterVO out of START_ARRAY token
Java调用以获取JSON:
ResponseEntity<FilterVO> responseEntity = restTemplate.exchange(url, HttpMethod.GET, entity, FilterVO.class);
要映射JSON的Java Bean:
public class FilterVO {
private String userName;
private String expirationTime;
private String lastMatchedTime;
private String state;
private Integer matchedTodayCount;
private Boolean useRegEx;
private List<String> matchStrings;
private List<String> hosts;
private String activationTime;
private String objectId;
private String description;
public Map<String, String> nameValuePairs;
getters and setters....
}
JSON我想要得到的东西:
[{
"userName": "Z111111",
"expirationTime": "2015-03-15T14:00:00.000-0400",
"activationTime": "1969-12-31T19:00:00.000-0500",
"hosts": ["aa", "bb"],
"matchStrings": [" aa is not running", " bb is not running"],
"useRegEx": false,
"nameValuePairs": {
"KEY VALUE": "asasa",
"PROGRAM": "dsds",
"REGION": "dfsffs"
},
"objectId": "SOMEID:20141015140803579-802",
"matchedTodayCount": 190,
"lastMatchedTime": "2015-03-09T07:53:20.366-0400",
"state": "ACTIVE",
"description": "Blah Blah Blah..."
}]
最初,由于一个元素Array的反序列化,我认为我正面临这个问题。所以我修改了我的java代码。
private void getFilter() {
RestTemplate restTemplate = getCustomRestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
headers.set("access-token", getAccessToken());
HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);
ResponseEntity<FilterVO> responseEntity = restTemplate.exchange(uri, HttpMethod.GET, entity, FilterVO.class);
}
private RestTemplate getCustomRestTemplate() {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
MappingJacksonHttpMessageConverter messageConverter = new MappingJacksonHttpMessageConverter();
messageConverter.setObjectMapper(mapper);
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
messageConverters.add(messageConverter);
RestTemplate restTemplate = new RestTemplate();
restTemplate.setMessageConverters(messageConverters);
return restTemplate;
}
我仍然遇到同样的错误。
我在类路径中有以下jackson
个罐子:
jackson-annotations-2.5.1.jar
jackson-core-2.5.1.jar
jackson-core-asl-1.9.13.jar
jackson-databind-2.5.1.jar
jackson-datatype-joda-2.5.1.jar
jackson-mapper-asl-1.9.13.jar
完整堆栈跟踪:
Exception in thread "main" org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Can not deserialize instance of com.zee.FilterVO out of START_ARRAY token
at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@bc5596; line: 1, column: 1]; nested exception is org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of com.zee.FilterVO out of START_ARRAY token
at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@bc5596; line: 1, column: 1]
at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readJavaType(MappingJacksonHttpMessageConverter.java:179)
at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.read(MappingJacksonHttpMessageConverter.java:171)
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:94)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:693)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:679)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:496)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:465)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:416)
at com.fidelity.selfservice.test.JMagicRestAPI.getFilter(JMagicRestAPI.java:63)
at com.fidelity.selfservice.test.JMagicRestAPI.main(JMagicRestAPI.java:46)
Caused by: org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of com.zee.FilterVO out of START_ARRAY token
at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@bc5596; line: 1, column: 1]
at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163)
at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:219)
at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:212)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromArray(BeanDeserializer.java:875)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:597)
at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2732)
at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1923)
at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readJavaType(MappingJacksonHttpMessageConverter.java:176)
... 9 more
答案 0 :(得分:4)
问题是您正在获取JSON数组,但是您尝试使用POJO反序列化该JSON,FilterVO
。
尝试更改此行:
ResponseEntity<FilterVO> responseEntity =
restTemplate.exchange(url, HttpMethod.GET, entity, FilterVO.class);
这一个:
ResponseEntity<List<FilterVO>> responseEntity =
restTemplate.exchange(url, HttpMethod.GET, entity,
new ParameterizedTypeReference<List<FilterVO>>() {});
现在您有一个List<FilterVO>
可以使用。