simple - libreria json java
No se puede asignar JSON a JSON Java (1)
El problema es que estás obteniendo una matriz JSON, pero estás tratando de deserializar esa JSON con un POJO, FilterVO
en tu caso.
Intenta cambiar esta línea:
ResponseEntity<FilterVO> responseEntity =
restTemplate.exchange(url, HttpMethod.GET, entity, FilterVO.class);
por este:
ResponseEntity<List<FilterVO>> responseEntity =
restTemplate.exchange(url, HttpMethod.GET, entity,
new ParameterizedTypeReference<List<FilterVO>>() {});
Ahora tendrá una List<FilterVO>
con la que trabajar.
Estoy recibiendo un error:
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
Llamada Java para obtener JSON:
ResponseEntity<FilterVO> responseEntity = restTemplate.exchange(url, HttpMethod.GET, entity, FilterVO.class);
Java Bean para asignar 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 lo que se supone que debo obtener:
[{
"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..."
}]
Inicialmente pensé que me enfrentaba a este problema debido a la deserialización de un elemento de Array. Así que modifiqué mi código 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;
}
Aún estoy obteniendo el mismo error.
Tengo los siguientes jarrones jackson
en mi classpath:
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
Rastreo completo de APILAMIENTO:
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