无法反序列化嵌套JSON数组Spring Resttemplate

mdfafbf1  于 2023-01-14  发布在  Spring
关注(0)|答案(1)|浏览(194)

我无法使用SpringRest模板从响应JSON反序列化嵌套的JSON数组。
我正在使用的JSON响应如下所示

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

和我的模型类如下

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(){
    }   
}

我使用rest模板调用端点,如下所示。

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

问题是在通过resttemplate接收到的响应中,List systemCall对象始终为空,即使每当我通过swagger点击端点时,systemCall属性出现在JSON中。

nbnkbykc

nbnkbykc1#

www.example.com中有一个缺陷RestTemplate.exchange,它甚至阻止了中等复杂JSON对象的反序列化。
将响应作为字符串读取,然后使用com.fasterxml.jackson.databind.ObjectMapper示例反序列化为List<ResolutionVo>,如下所示:

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>>() {});

我认为这是一个相关的问题。

相关问题