gpt4 book ai didi

java - Lombok 未创建默认构造函数导致 jackson-databinding 失败

转载 作者:行者123 更新时间:2023-11-30 02:00:35 26 4
gpt4 key购买 nike

我有一个 POJO

@Data
@AllArgsConstrcutor
class Test {

T field1,
T field2,
}

当我尝试使用 jackson-databind 2.9.3 反序列化它时,它是

failing com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of com.mypackage.Test (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator) at [Source: (String)

最佳答案

@AllArgsConstructor 为类的每个字段创建一个带有参数的构造函数(因此,在您的情况下,您将得到 Test(T, T))。

对于默认构造函数,您应该添加@NoArgsConstructor注释:

@Data
@AllArgsConstrcutor // Won't be used by Jackson, but there's no harm in it, per-se
@NoArgsConstructor // Here!
class Test {
T field1;
T field2;
}

关于java - Lombok 未创建默认构造函数导致 jackson-databinding 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52982501/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com