如何在springboot中为Json构建java Pojo
我有以下Json,并希望使用SpringWebClient mono响应将此Json映射为JavaPOJO。 在这个json密钥中,data-1和en不是常量,并且不断变化。例如,它可以是data-2和fr或data-3和it如何在springboot中为Json构建java Pojo,java,json,jackson,spring-webclient,Java,Json,Jackson,Spring Webclient,我有以下Json,并希望使用SpringWebClient mono响应将此Json映射为JavaPOJO。 在这个json密钥中,data-1和en不是常量,并且不断变化。例如,它可以是data-2和fr或data-3和it { "data": { "attributes": { "data-1": { "en": [
{
"data": {
"attributes": {
"data-1": {
"en": [
{
"id": 1,
"code": "GI",
"order": 10
},
{
"id": 2,
"code": "TH",
"order": 10
}
]
}
}
}
}
DTO结构:
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ListData {
private Map<String, Attributes> data;
}
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Attributes {
private Map<String, Map<String, MyList>> attributes;
}
@JsonInclude(JsonInclude.Include.NON_NULL)
public class MyList {
private List<Entry> entryList;
}
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Entry {
private String id;
private String code;
private String order;
}
错误如下:
org.springframework.core.codec.CodecException: Type definition error: [simple type, class com.test.ListAttributes]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.test.ListAttributes` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
有人能帮我解决这个问题吗,怎么解决。{
“数据”:{
“属性”:{
“数据-1”:{
“en”:[
{
“id”:1,
“代码”:“GI”,
“命令”:10
},
{
“id”:2,
“代码”:“TH”,
“命令”:10
}
]
}
}
}
}
“data-1”和“en”作为变量键意味着需要一个双重嵌套的映射。内部映射将字符串映射到对象的列表
(条目
特定情况下的对象)
因此,上述JSON的结构/类型映射到Java类型:
类容器{
公共数据;
公共容器(){}
}
类数据{
公共地图属性;
公共数据(){}
}
班级报名{
公共长id;
公共字符串代码;
公共秩序;
公愤(){}
}
确保您的类型是默认可构造的,并且其字段可以由Jackson设置为什么它要尝试构造ListAttributes
?我在你的代码中没有看到这个类。我重命名了所有的类,但没有错误。它实际上是attribtues,而您显示的类是真正的类?没有getter/setter,即只有无法访问的私有字段?错误消息明确指出“没有创建者,就像默认构造函数一样”,我在问题代码中没有看到任何自定义构造函数。您是否已经定义了构造函数(因此禁用了无参数的默认构造函数)并将其从问题中排除?再看一遍,我认为您的DTO嵌套太深了。属性
值不会映射到包含列表的对象,而是直接映射到列表
org.springframework.core.codec.CodecException: Type definition error: [simple type, class com.test.ListAttributes]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.test.ListAttributes` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)