kumar Anny
kumar Anny

Reputation: 366

RestTemplate getForObject is not able to map Custom Class

Custom class structure DocumentListVO mapping one to one with JSON response however while using DocumentListVO dv1 = restTemplate.getForObject(uri,DocumentListVO.class), it is throwing error with following stack trace:

GET request for
"http://aaa.ddd.com:8081/bpi1/service/aa/documen1t/list23"
resulted in 200 ()
    [2018-05-28 12:49:46,397]-DEBUG org.springframework.web.client.RestTemplate  - Reading [class
com.fascorp.isis.ejb.documentVO.DocumentListVO1] as
"application/json;charset=UTF-8" using
[org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@58206f8]
    [2018-05-28 12:49:46,399]-DEBUG org.apache.http.wire  - http-outgoing-0 << "0[\r][\n]"
    [2018-05-28 12:49:46,400]-DEBUG org.apache.http.wire  - http-outgoing-0 << "[\r][\n]"
    [2018-05-28 12:49:46,402]-DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager  -
Connection [id: 0][route: {}->http://aaa.ddd.com:8081] can be kept
alive indefinitely
    [2018-05-28 12:49:46,404]-DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager  -
Connection released: [id: 0][route:
{}->http://aaa.ddd.com:8081][total kept alive: 1; route allocated:
1 of 2; total allocated: 1 of 10]
    org.springframework.http.converter.HttpMessageNotReadableException:
Could not read JSON: Unrecognized field "Results" (class
com.fascorp.isis.ejb.documentVO.DocumentListVO1), not marked as
ignorable (3 known properties: , "success", "results", "count"])
     at [Source: org.apache.http.conn.EofSensorInputStream@209152d; line: 1, column: 13] (through reference chain:
com.fascorp.isis.ejb.documentVO.DocumentListVO1["Results"]); nested
exception is
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:
Unrecognized field "Results" (class
com.fascorp.isis.ejb.documentVO.DocumentListVO1), not marked as
ignorable (3 known properties: , "success", "results", "count"])
     at [Source: org.apache.http.conn.EofSensorInputStream@209152d; line: 1, column: 13] (through reference chain:
com.fascorp.isis.ejb.documentVO.DocumentListVO1["Results"])
            at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.readJavaType(MappingJackson2HttpMessageConverter.java:228)
            at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.read(MappingJackson2HttpMessageConverter.java:220)
            at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:95)
            at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:559)
            at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:527)
            at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:259)
public class DocumentListVO 
{
  String Count;
  boolean Success;
  Results[] Results;
  
  public DocumentListVO()
  {
    super();
  }

  public DocumentListVO(String Count, boolean success, Results[] results)
  {
    super();
    this.Count = Count;
    this.Success = success;
    this.Results = results;
  }
  
  
  public Results[] getResults()
  {
    return Results;
  }
  public void setResults( Results[] results)
  {
    this.Results = results;
  }
  public String getCount()
  {
    return Count;
  }
  public void setCount( String count)
  {
    this.Count = count;
  }
  public boolean isSuccess()
  {
    return Success;
  }
  public void setSuccess( boolean success)
  {
    this.Success = success;
  }
  
  @Override
  public String toString()
  {
    return "DocumentListVO [count=" + Count + ", success=" + Success
           + ", results=" + Arrays.toString(Results) + "]";
  }
}

JSON response:

{
    "Results": [
        {
            "Category": "abcde",
            "DateCreated": "12-13-2016 14:43:31",
            "Token": "ad",
            "ClassDescription": "Wisd",
            "DocumentTitle": "Rela",
            "ContentSize": "58",
            "MimeType": "application/pdf"
        },
        {
            "Category": "tyreytr",
            "DateCreated": "11-07-2017 13:37:11",
            "Token": "hgdf",
            "ClassDescription": "With",
            "DocumentTitle": "Related Doc - 1234646315",
            "ContentSize": "58",
            "MimeType": "application/pdf"
        }
    ]
    "Count": 89,
    "Success": true
}

Upvotes: 2

Views: 2599

Answers (1)

Hemant Patel
Hemant Patel

Reputation: 3260

Add @JsonProperty annotation on your fields.

public class DocumentListVO 
{

    @JsonProperty("Count")
    String Count;

    @JsonProperty("Success")
    boolean Success;

    @JsonProperty("Results")
    Results[] Results;

    ..... setters / getters
    // add annotation on setter / getter too (not necessary)

}

The reason is jackson doesn't reads the private variable names, but setter / getter method names.
So a setter for Results will be setResults but jackson interpret the field name as results as per naming convention of setters / getters.
After adding @JsonProperty you can rename your fields as per java naming convention as well, count, success, results etc.

Upvotes: 5

Related Questions