7

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.

  • Does this answer your question? [JSON parse error: Can not construct instance of io.starter.topic.Topic](https://stackoverflow.com/questions/48448079/json-parse-error-can-not-construct-instance-of-io-starter-topic-topic) – Rohde Fischer Oct 16 '20 at 11:23

2 Answers2

14

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

Simon Martinelli
  • 34,053
  • 5
  • 48
  • 82
1

This has already been answered here: JSON parse error: Can not construct instance of io.starter.topic.Topic

In general design wise, I would strongly recommend going for the second solution with annotating the constructor, rather than a default argument constructor. That way you can have immutable fields (make them final) and ensure that you don't accept nulls. In 2020 I would consider any design using a default constructor due to serialization legacy-design.

Rohde Fischer
  • 1,248
  • 2
  • 10
  • 32