无法将 JSON 映射到 Java bean
Unable to Map JSON to Java bean
我遇到错误:
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);
Java Bean to Map JSON:
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..."
}]
Initially I thought I am facing this issue because of deserialization of one element Array. So I modified my java code.
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
问题是您获得了一个 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>
可以使用。
我遇到错误:
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);
Java Bean to Map JSON:
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..."
}]
Initially I thought I am facing this issue because of deserialization of one element Array. So I modified my java code.
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
问题是您获得了一个 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>
可以使用。