如何让杰克逊忽略构造函数属性
How to get Jackson to ignore constructorproperties
我正在尝试让 Jackson 反序列化
{
"test": 2018
}
到
SomeJavaClass:
private final Test test
但我想使用 Project Lombok 进行测试 class。然而,Lombok 使用 ConstructorProperties 注释 class,由于某种原因,这使得 jackson 失败。
我的 classes 看起来像这样:
@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 以某种方式忽略构造函数属性?
失败的原因不是 @ConstructorProperties
。事实上,为了让 Jackson 与 Lombok 的 @AllArgsConstructor
.
一起工作,这个注释 是必需的
这里的问题是你的JSON中test
的值是一个整数,但是你的class结构要求它是一个对象。因此,您必须将 SomeJavaClass
中的字段 test
设为 int
。那么你也不需要 Test
class。 (或者在 Test
中将 value
重命名为 test
并删除 SomeJavaClass
并反序列化为 Test
。)
我也觉得这里的问题不是注解@java.beans.ConstructorProperties({"value"})
.
根据你的 delombok,你似乎有一组注释可以防止默认构造函数的形成。
所以也许您可以通过添加 @NoArgsConstructor
来解决这个问题。如果没有默认构造函数并且没有 @JsonCreator
s,您将遇到如下错误:
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)
我正在尝试让 Jackson 反序列化
{
"test": 2018
}
到
SomeJavaClass:
private final Test test
但我想使用 Project Lombok 进行测试 class。然而,Lombok 使用 ConstructorProperties 注释 class,由于某种原因,这使得 jackson 失败。
我的 classes 看起来像这样:
@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 以某种方式忽略构造函数属性?
失败的原因不是 @ConstructorProperties
。事实上,为了让 Jackson 与 Lombok 的 @AllArgsConstructor
.
这里的问题是你的JSON中test
的值是一个整数,但是你的class结构要求它是一个对象。因此,您必须将 SomeJavaClass
中的字段 test
设为 int
。那么你也不需要 Test
class。 (或者在 Test
中将 value
重命名为 test
并删除 SomeJavaClass
并反序列化为 Test
。)
我也觉得这里的问题不是注解@java.beans.ConstructorProperties({"value"})
.
根据你的 delombok,你似乎有一组注释可以防止默认构造函数的形成。
所以也许您可以通过添加 @NoArgsConstructor
来解决这个问题。如果没有默认构造函数并且没有 @JsonCreator
s,您将遇到如下错误:
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)