Shivkumar Mallesappa
Shivkumar Mallesappa

Reputation: 3077

Spring Boot RestTemplate deserialization issue

I have following Class

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

}

I am trying to hit my another REST API (GET REQUEST) and map the response to this Class.

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

But I am getting the following error :

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

Upvotes: 1

Views: 2560

Answers (1)

Afridi
Afridi

Reputation: 6922

For response to be mapped to your custom DTO, you should have default constructor for given DTO. In RestResponseDto there is no default constructor defined. So change it to:

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
}

Upvotes: 1

Related Questions