Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to Map JSON to Java bean

I am getting 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

Java call to get 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 what I am suppose to get:

[{
    "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;
}

Still I am getting the same error.

I am having the following jackson jars in my 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

Full STACK Trace:

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
like image 817
Zeeshan Avatar asked Sep 29 '22 13:09

Zeeshan


1 Answers

The problem is that you're getting a JSON array, but you're trying to deserialize that JSON with a POJO, FilterVO in your case.

Try changing this line:

ResponseEntity<FilterVO> responseEntity = 
    restTemplate.exchange(url, HttpMethod.GET, entity, FilterVO.class);

by this one:

ResponseEntity<List<FilterVO>> responseEntity = 
    restTemplate.exchange(url, HttpMethod.GET, entity, 
    new ParameterizedTypeReference<List<FilterVO>>() {});

Now you'll have a List<FilterVO> to work with.

like image 122
fps Avatar answered Oct 17 '22 13:10

fps