英文:
com.fasterxml.jackson.databind.JsonMappingException: no suitable constructor found, can not deserialize from Object value
问题
我在映射响应时遇到了标题错误:
Retention.java
@Getter
@Setter
@Builder
public class Retention {
private int min_age_days;
private int max_age_days;
private boolean auto_prune;
}
我正在使用Java + lombok
完整的堆栈跟踪:
com.fasterxml.jackson.databind.JsonMappingException: 无法构造getChannelInfo.Retention的实例:找不到合适的构造函数,无法从对象值反序列化(缺少默认构造函数或创建者,或者可能需要添加/启用类型信息?)
在 [Source: {"id":"53","href":"https://<ip address:port>/api/v1/channel/53","public_read":true,"public_write":true,"sequenced":true,"locked":false,"head":0,"retention":{"min_age_days":0,"max_age_days":0,"auto_prune":true},"access_tokens":[{"id":"58","token":"","description":"Owner","can_read":true,"can_write":true}]}; line: 1, column: 160] 中(通过引用链:service.xxxx["retention"])
我的JSON如下所示:
{
"id": "53",
"href": "https://161.35.164.133:5011/api/v1/channel/53",
"public_read": true,
"public_write": true,
"sequenced": true,
"locked": false,
"head": 0,
"retention": {
"min_age_days": 0,
"max_age_days": 0,
"auto_prune": true
},
"access_tokens": [
{
"id": "58",
"token": "DAH-9_5dwid6PIBjtHjBdl3PwTVD3qh53ZWddSCfw-eQOyY4MRyR8ZolmARU2q2lGyoN7oD74cwWQHHANkJDAw",
"description": "Owner",
"can_read": true,
"can_write": true
}
]
}
英文:
I'm getting the Title error while mapping the response:
Retention.java
@Getter
@Setter
@Builder
public class Retention {
private int min_age_days;
private int max_age_days;
private boolean auto_prune;
}
I'm using Java + lombok
Full stacktrace:
com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of getChannelInfo.Retention: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)
at [Source: {"id":"53","href":"https://<ip address:port>/api/v1/channel/53","public_read":true,"public_write":true,"sequenced":true,"locked":false,"head":0,"retention":{"min_age_days":0,"max_age_days":0,"auto_prune":true},"access_tokens":[{"id":"58","token":"","description":"Owner","can_read":true,"can_write":true}]}; line: 1, column: 160] (through reference chain: service.xxxx["retention"])
My JSON looks like below:
{
"id": "53",
"href": "https://161.35.164.133:5011/api/v1/channel/53",
"public_read": true,
"public_write": true,
"sequenced": true,
"locked": false,
"head": 0,
"retention": {
"min_age_days": 0,
"max_age_days": 0,
"auto_prune": true
},
"access_tokens": [
{
"id": "58",
"token": "DAH-9_5dwid6PIBjtHjBdl3PwTVD3qh53ZWddSCfw-eQOyY4MRyR8ZolmARU2q2lGyoN7oD74cwWQHHANkJDAw",
"description": "Owner",
"can_read": true,
"can_write": true
}
]
}
答案1
得分: 2
你需要为该类定义一个构造函数。
@Getter
@Setter
@Builder
@NoArgsConstructor
public class Retention {
private int min_age_days;
private int max_age_days;
private boolean auto_prune;
}
请注意,我在类中添加了@NoArgsConstructor
。
英文:
You need to define a constructor to the class.
@Getter
@Setter
@Builder
@NoArgsConstructor
public class Retention {
private int min_age_days;
private int max_age_days;
private boolean auto_prune;
}
Notice that I added @NoArgsConstructor to the class.
答案2
得分: 1
添加注解@NoArgsConstructor
和@AllArgsConstructor
objectmapper
调用默认构造函数首先创建POJO的实例。然后解析JSON中的每个条目,并使用setter方法将其设置在实例中。由于您没有默认构造函数,所以它失败并引发了适当的异常。@NoArgsConstructor
注解提供了默认构造函数,并使其正常工作。
英文:
Add annotations @NoArgsConstructor
and @AllArgsConstructor
objectmapper
calls the default constructor to create the instance of the POJO first. Then each entry from the JSON is parsed and set in the instance using setters. Since you didn't have the default constructor, it failed with an appropriate exception. @NoArgsConstructor
annotation provides the default constructor and it makes it work.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论