Results 1 to 5 of 5

Thread: Getting a "out of START_OBJECT token" deserialization issue‏

  1. #1

    Default Getting a "out of START_OBJECT token" deserialization issue‏

    Hi all,

    I'm encountering an issue where my deserialization fails. I'm not sure if it is Spring-related, Jackson-related, or my own POJO/JSON mapping, but let me describe the details below.

    The error message is:

    Code:
    I/O error: Can not deserialize instance of co.test.location.status.Request[] out of START_OBJECT token at [Source: org.apache.http.conn.EofSensorInputStream@45ce7738; line: 1, column: 1]
    Below is the sample JSON string to deserialize (notice the Pascal case for the property names):
    Code:
     {"Category":"Test","DateClosed":"","DateInitiated":"3\/20\/2011","Description":"","FullAddress":"2301  COURTKING DR","Intersection":"COURTKING DR AND HENDERSON PL","QuestionAndAnswers":[{"Answer":"Depends on the sign","Question":"Is this Deliverable?"}],"RequestType":"Deliver Type","ServiceRequestNumber":"213-375001","Status":"Open","SubRequestType":"Sign Visibility"}
    Below is the POJO to represent the JSON data:
    Code:
    public class Request {
        @JsonProperty("Category")
        private String category;
        @JsonProperty("ServiceRequestNumber")
        private String serviceRequestNumber;
        @JsonProperty("Status")
        private String status;
        @JsonProperty("DateInitiated")
        private Date dateInitiated;
        @JsonProperty("DateClosed")
        private Date dateClosed;
        @JsonProperty("RequestType")
        private String requestType;
        @JsonProperty("SubRequestType")
        private String subRequestType;
        @JsonProperty("FullAddress")
        private String fullAddress;
        @JsonProperty("Description")
        private String description;
        @JsonProperty("Intersection")
        private String intersection;
        @JsonProperty("QuestionAndAnswers")
        private List<QuestionAndAnswers> questionAndAnswers = new ArrayList<QuestionAndAnswers>();
        ...
       ...
       // getters and setters
    
       // static inner class
       public static class QuestionAndAnswers {
    
            @JsonProperty("Question")
            private String question;
            @JsonProperty("Answer")
            private String answer;
           
            // getters and setters
    }
    The fail call is via Spring's RestTemplate:
    Code:
    RestTemplate restTemplate = new RestTemplate();
    
    List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
    messageConverters.add(new FormHttpMessageConverter());
    messageConverters.add(new StringHttpMessageConverter());
    messageConverters.add(new MappingJacksonHttpMessageConverter());
    restTemplate.setMessageConverters(messageConverters);
    					    
    ResponseEntity<Request[]> responseEntity = restTemplate.exchange(url, HttpMethod.GET, createHttpRequestEntity(), Request[].class);

    My hunch is the POJO is incorrectly defined. Because of the first letter uppercasing in the JSON properties, I had to add the @JsonProperty annotations to make the conversion. Not sure if this is the right way or not.

    Any help would be appreciated. Thanks.

    -los

  2. #2

    Default Re: Getting a "out of START_OBJECT token" deserialization issue‏

    "My hunch is the POJO is incorrectly defined."

    Yes, and unfortunately, the "out of START_OBJECT token" seems to be a catch-all for any deserialization issues.

    If you have control over the JSON reponse, remove both the QuestionAndAnswers section in your POJO and that section in the JSON response. Usually, that is were I find most of my problems. If that works, add both of those back in but change the collection from ArrayList to HashMap.

    Quite often, when I run into it, I scale back my POJO to simple Strings, get that working and start adding the more exotic stuff like collections or references to other complex POJO classes.

    Perry Hoekstra

  3. #3

    Default

    Hi,

    Thanks for the reply. I don't have any control of the JSON response. However, I don't see how a Map will work... I mean if I used a map, what would be my key/value? Is it Map<String, String>? If this is the case, then I won't need the inner class.

  4. #4

    Default

    Hi,

    Just have an update. The reason why its not derserializing is because the JSON response happens to be a redirect URL:

    Code:
    <HTML><HEAD><TITLE> Web Authentication Redirect</TITLE><META http-equiv="Cache-control" content="no-cache"><META http-equiv="Pragma" content="no-cache"><META http-equiv="Expires" content="-1"><META http-equiv="refresh" content="1; URL=https://1.1.1.1/login.html?redirect=loc.regist.co/LocationLookup/proxy.ashx?http://epor01/LocationLookupService/LocationLookupService.svc/Request/213-375001?output=json"></HEAD></HTML>
    I don't know why the server is sending this back instead of doing the redirect and then sending me the true JSON data. Anyway, how would I handle this issue (or circumvent it, if possible)?

    Thanks.

    -los

  5. #5

    Default Re: Getting a "out of START_OBJECT token" deserialization issue‏

    Catch the runtime RestClientException

    Code:
    try {
        ResponseEntity<Request[]> responseEntity = restTemplate.exchange(url, HttpMethod.GET, createHttpRequestEntity(), Request[].class);
    }
    catch (RestClientException restException) {
      // examine the restException
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •