JSON parse error: Cannot construct instance of `com.dto.IdDTO` (although at least one Creator exists)
JSON parse error: Cannot construct instance of `com.dto.IdDTO` (although at least one Creator exists)
我有一个 Spring 启动应用程序,使用 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;
}
}
和
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;
}
}
我调用了一个 REST Web 服务,我看到我从 Postman 收到(正确)了以下响应:
{
"errorCode": null,
"id": 3534016
}
但是,当我检索响应时,出现以下异常:
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]
有人知道为什么应用程序无法将收到的 JSON 映射到对象吗?
P.S。 1) 我还有其他扩展 BasicResponseDTO 的 DTO,反序列化对它们来说工作正常。
P.S。 2) 类 的定义在服务器端和客户端都是一样的。
IdDTO 上没有默认构造函数。只有一个采用 id:
public IdDTO(long id) {
this.id = id;
}
你必须加一个:
public IdDTO() {
}
JSON 反序列化需要从您的 类
构造对象
这里已经回答了:JSON parse error: Can not construct instance of io.starter.topic.Topic
在一般设计方面,我强烈建议使用注释构造函数而不是默认参数构造函数的第二种解决方案。这样你就可以拥有不可变的字段(使它们成为 final
)并确保你不接受空值。在 2020 年,由于序列化 legacy-design.
,我会考虑使用默认构造函数的任何设计
我有一个 Spring 启动应用程序,使用 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;
}
}
和
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;
}
}
我调用了一个 REST Web 服务,我看到我从 Postman 收到(正确)了以下响应:
{
"errorCode": null,
"id": 3534016
}
但是,当我检索响应时,出现以下异常:
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]
有人知道为什么应用程序无法将收到的 JSON 映射到对象吗?
P.S。 1) 我还有其他扩展 BasicResponseDTO 的 DTO,反序列化对它们来说工作正常。
P.S。 2) 类 的定义在服务器端和客户端都是一样的。
IdDTO 上没有默认构造函数。只有一个采用 id:
public IdDTO(long id) {
this.id = id;
}
你必须加一个:
public IdDTO() {
}
JSON 反序列化需要从您的 类
构造对象这里已经回答了:JSON parse error: Can not construct instance of io.starter.topic.Topic
在一般设计方面,我强烈建议使用注释构造函数而不是默认参数构造函数的第二种解决方案。这样你就可以拥有不可变的字段(使它们成为 final
)并确保你不接受空值。在 2020 年,由于序列化 legacy-design.