【发布时间】:2023-04-10 12:40:01
【问题描述】:
更新后反序列化失败。
我将我的微服务从Spring 1.5.10.RELEASE 更新为Spring 2.0.3.RELEASE,并将lombok 从1.16.14 更新为1.18.0 和jackson-datatype-jsr310 从2.9.4 更新为2.9.6。
JSON 字符串 -
{"heading":"Validation failed","detail":"field must not be null"}
班级 -
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class ErrorDetail {
private final String heading;
private final String detail;
private String type;
}
方法调用-
ErrorDetail errorDetail = asObject(jsonString, ErrorDetail.class);
用于反序列化的方法-
import com.fasterxml.jackson.databind.ObjectMapper;
// more imports and class defination.
private static <T> T asObject(final String str, Class<T> clazz) {
try {
return new ObjectMapper().readValue(str, clazz);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
错误 -
java.lang.RuntimeException: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.foo.bar.ErrorDetail` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
at [Source: (String)"{"heading":"Validation failed","detail":"field must not be null"}"; line: 1, column: 2]
【问题讨论】:
-
您是否尝试将构造函数添加到 ErrorDetail 类?
标签: java spring-boot jackson lombok jackson-databind