2017-08-28 41 views
0

我有以下春季啓動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請求),並映射到這個類的響應。

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?); 
+0

您有缺省的無參數構造函數嗎? –

回答

0

爲了響應映射到您的自定義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 
}