Search code examples
jsonspring-bootrestjava-11payara

JSON parse error: Cannot construct instance of `com.dto.IdDTO` (although at least one Creator exists)


I have a Spring Boot application using version 2.2.4 and Zulu Java 11.0.5 from Azul. It is accessing a REST web service which is deployed on a Payara web server (version 5.194).

I am using the following DTOs:

public class IdDTO extends BasicResponseDTO {
    private long id;

    public IdDTO(long id) {
        this.id = id;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

}

and

public class BasicResponseDTO implements Serializable {

    private String errorCode;

    public BasicResponseDTO() {
        this.setErrorCode(null);
    }

    public String getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

}

I invoke a REST web service and from Postman I see I receive (correctly) the following response:

{
    "errorCode": null,
    "id": 3534016
}

However, when I retrieve the response, I get the following exception:

class org.springframework.web.client.RestClientException/Error while extracting response for type [class com.dto.IdDTO] and content type [application/json;charset=UTF-8]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of `com.dto.IdDTO` (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `com.dto.IdDTO` (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator)
 at [Source: (PushbackInputStream); line: 1, column: 2]

Does anyone have any idea on why the application is not able to map the received JSON to the object?

P.S. 1) I also have other DTOs that extend BasicResponseDTO and the de-serialization works fine for them.

P.S. 2) The definition of the classes is the same on both the server and the client.


Solution

  • There is no default constructor on IdDTO. Only one that takes id:

    public IdDTO(long id) {
        this.id = id;
    }
    

    You have to add one:

    public IdDTO() {
    }
    

    This is needed by JSON deserialization to construct objects from your classes