Spring Boot RestTemplate反序列化问题

时间:2017-08-28 10:12:59

标签: spring-boot resttemplate

我有以下课程

public class RestResponseDto {

    private Boolean status;
    private Object data;
    private Object error;


    public RestResponseDto(Boolean status, Object data, Object error) {
        this.status = status;
        this.data = data;
        this.error = error;
    }

         //Getters and setters

}

我正在尝试点击另一个REST API(GET REQUEST)并将响应映射到此类。

RestTemplate restTemplate = new RestTemplate();
        RestResponseDto result = restTemplate.getForObject(uri, RestResponseDto.class);

但是我收到以下错误:

Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Can not construct instance of xxx.xxx.xxx.xxx: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?);

1 个答案:

答案 0 :(得分:1)

要将响应映射到自定义DTO,您应该具有给定DTO的默认构造函数。在RestResponseDto中,没有定义默认构造函数。所以改成它:

public class RestResponseDto {

    private Boolean status;
    private Object data;
    private Object error;

    public RestResponseDto() {
    }
    public RestResponseDto(Boolean status, Object data, Object error) {
        this.status = status;
        this.data = data;
        this.error = error;
    }
         //Getters and setters
}