我正在尝试让 Jackson 反序列化
{
"test": 2018
}
到
SomeJavaClass:
private final Test test
但我想使用 Project Lombok 来制作我的测试类。然而,Lombok 使用 ConstructorProperties 注释该类,由于某种原因,这使得 Jackson 失败。
我的类(class)如下所示:
@Value
public class SomeJavaClass {
Test test;
}
@Value
public class Test{
String value;
}
测试被分解为:
public class Test {
int value;
@java.beans.ConstructorProperties({"value"})
public Test(final int value) {
this.value = value;
}
public int getValue() {
return this.value;
}
public boolean equals(final Object o) {
if (o == this) {
return true;
}
if (!(o instanceof Test)) {
return false;
}
final Test other = (Test) o;
if (this.getValue() != other.getValue()) {
return false;
}
return true;
}
public int hashCode() {
final int PRIME = 59;
int result = 1;
result = result * PRIME + this.getValue();
return result;
}
public String toString() {
return "Test(value=" + this.getValue() + ")";
}
}
是否有可能让 Jackson 以某种方式忽略构造函数属性?
最佳答案
我还认为这里的问题不是注释@java.beans.ConstructorProperties({"value"})
。
根据您的delombok,您似乎有一组注释可以阻止默认构造函数的形成。
所以也许你可以通过添加@NoArgsConstructor来摆脱这个问题。如果没有默认构造函数并且没有 @JsonCreator
,您将收到如下错误:
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of org.example.spring.jackson.JacksonTest$TestClass (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator)
关于java - 如何让 Jackson 忽略构造函数属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53261915/