com.fasterxml.jackson.databind.jsonmappingexception:找不到合适的构造函数,无法从对象值反序列化

fjaof16o  于 2021-07-05  发布在  Java
关注(0)|答案(2)|浏览(428)

这个问题在这里已经有答案了

jackson错误:没有适合简单类的构造函数(2个答案)
4个月前关门了。
Map响应时出现标题错误: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: 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"])

我的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
    }
]

}

2uluyalo

2uluyalo1#

您需要为类定义一个构造函数。

@Getter
@Setter
@Builder
@NoArgsConstructor
public class Retention {
    private int min_age_days;
    private int max_age_days;
    private boolean auto_prune;
}

注意,我向类中添加了@noargsconstuctor。

voase2hg

voase2hg2#

添加注解 @NoArgsConstructor 以及
@AllArgsConstructor objectmapper 首先调用默认构造函数来创建pojo的示例。然后,json中的每个条目都被解析,并使用setter在示例中进行设置。由于您没有默认构造函数,因此它失败并出现相应的异常。 @NoArgsConstructor 注解提供了默认的构造器并使其工作。

相关问题