JSON 解析错误:无法构造`com.dto.IdDTO` 的实例(尽管至少存在一个 Creator)

And*_*aru 4 rest json spring-boot payara java-11

我有一个 Spring Boot 应用程序,使用 Azul 的 2.2.4 版和 Zulu Java 11.0.5。它正在访问部署在 Payara Web 服务器(版本 5.194)上的 REST Web 服务。

我正在使用以下 DTO:

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;
    }

}
Run Code Online (Sandbox Code Playgroud)

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;
    }

}
Run Code Online (Sandbox Code Playgroud)

我调用了一个 REST web 服务,从 Postman 那里我看到我收到(正确)以下响应:

{
    "errorCode": null,
    "id": 3534016
}
Run Code Online (Sandbox Code Playgroud)

但是,当我检索响应时,出现以下异常:

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]
Run Code Online (Sandbox Code Playgroud)

有没有人知道为什么应用程序无法将接收到的 JSON 映射到对象?

PS 1) 我还有其他扩展 BasicResponseDTO 的 DTO,反序列化对他们来说很好。

PS 2)类的定义在服务器和客户端上是相同的。

Sim*_*lli 6

IdDTO 上没有默认构造函数。只有一个需要 id:

public IdDTO(long id) {
    this.id = id;
}
Run Code Online (Sandbox Code Playgroud)

你必须添加一个:

public IdDTO() {
}
Run Code Online (Sandbox Code Playgroud)

这是 JSON 反序列化从您的类构造对象所必需的