javajsonspringspring-bootspring-resttemplate

Not able to deserialize nested JSON array Spring Resttemplate


I am not able to deserialize nested JSON array from response JSON using Spring Rest template.

JSON response which I am consuming is as follows

[
  {
    "creationTime": "2023-01-13",
    "code": "456",
    "cid": "123",
    "priority": "CRITICAL",
    "reviewDate": null,
    "systemCall": [
      {
        "creationTime": "2023-01-13",
        "status": null,
        "id": "787878",
        "modificationTime": "2023-01-13",
        "creatorId": "ABC"
      },
      {
        "creationTime": "2023-01-14",
        "status": null,
        "id": "787879",
        "modificationTime": "2023-01-14",
        "creatorId": "DEF"
      }
    ],
    level: "1"
  }
]

And My model classes as follows

public class Resolution {
    private String creationTime;
    private String code;
    private String cid;
    private String priority;
    private String reviewDate
    private List<SystemCallVo> systemCall;
    private String level;

    public Resolution(){
    } 
    
    //Getters and  Settrs
}

public class SystemCallVo {
    private String creationTime;
    private String status;
    private String id;
    private String modificationTime;
    private String creatorId;
   
    public SystemCallVo(){
    } 
    //Getters and  Setters
}

public class ResolutionVo extends Resolution{
    public ResolutionVo(){
    }   
}

I am calling my endpoint using rest template as follows.

ResponseEntity<List<ResolutionVo>> response = this.restTemplateConfig.restTemplate().exchange(builder.toUriString(), HttpMethod.POST, httpEntity, new ParameterizedTypeReference<List<ResolutionVo>>() {
            }, new Object[0]);

Problem is List systemCall object is always null in response received through resttemplate even though systemCall attribute is present in JSON whenever I hit endpoint through swagger.


Solution

  • There is a defect in RestTemplate.exchange that prevents the deserialisation of even moderately complex JSON objects.

    Read the response as a String and then deserialise to List<ResolutionVo> with an com.fasterxml.jackson.databind.ObjectMapper instance as follows:

    ResponseEntity<String> response = this.restTemplateConfig.restTemplate().exchange(builder.toUriString(), HttpMethod.POST, httpEntity, String.class, new Object[0]);
    String body = response.getBody();
    List<ResolutionVo> value = objectMapper.readValue(body, new TypeReference<List<ResolutionVo>>() {});
    

    I think this is a related issue.