java - com.fasterxml.jackson.databind.JsonMappingException : no suitable constructor found, 无法从对象值反序列化

标签 java lombok

<分区>

我在映射响应时遇到标题错误: 保留.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
    }
]

最佳答案

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

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

请注意,我在类中添加了@NoArgsConstructor。

关于java - com.fasterxml.jackson.databind.JsonMappingException : no suitable constructor found, 无法从对象值反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63725744/

相关文章:

java - 图像不会出现在 jar 中

java - org.apache.jasper.JasperException : Unable to initialize TldLocationsCache: java. util.zip.ZipException:无效的条目 CRC

java - 算术表达式

Java连接四胜利检查

带有@Builder.Default 的Java 记录

java - 在 java 10 中使用 lombok.Getter(lazy = true) 时出现不兼容类型错误

java - 浅拷贝 - 新实例或赋值

java - Lombok @Builder 继承解决方法

eclipse - Eclipse Oxygen 的 Lombok 问题

java - 如何使用 Jackson 反序列化外部 Lombok 构建器类