com.fasterxml.jackson.databind.JsonMappingException: no suitable constructor found, can not deserialize from Object value

huangapple go评论61阅读模式
英文:

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: {&quot;id&quot;:&quot;53&quot;,&quot;href&quot;:&quot;https://&lt;ip address:port&gt;/api/v1/channel/53&quot;,&quot;public_read&quot;:true,&quot;public_write&quot;:true,&quot;sequenced&quot;:true,&quot;locked&quot;:false,&quot;head&quot;:0,&quot;retention&quot;:{&quot;min_age_days&quot;:0,&quot;max_age_days&quot;:0,&quot;auto_prune&quot;:true},&quot;access_tokens&quot;:[{&quot;id&quot;:&quot;58&quot;,&quot;token&quot;:&quot;&quot;,&quot;description&quot;:&quot;Owner&quot;,&quot;can_read&quot;:true,&quot;can_write&quot;:true}]}; line: 1, column: 160] (through reference chain: service.xxxx[&quot;retention&quot;])

My JSON looks like below:

{
&quot;id&quot;: &quot;53&quot;,
&quot;href&quot;: &quot;https://161.35.164.133:5011/api/v1/channel/53&quot;,
&quot;public_read&quot;: true,
&quot;public_write&quot;: true,
&quot;sequenced&quot;: true,
&quot;locked&quot;: false,
&quot;head&quot;: 0,
&quot;retention&quot;: {
    &quot;min_age_days&quot;: 0,
    &quot;max_age_days&quot;: 0,
    &quot;auto_prune&quot;: true
},
&quot;access_tokens&quot;: [
    {
        &quot;id&quot;: &quot;58&quot;,
        &quot;token&quot;: &quot;DAH-9_5dwid6PIBjtHjBdl3PwTVD3qh53ZWddSCfw-eQOyY4MRyR8ZolmARU2q2lGyoN7oD74cwWQHHANkJDAw&quot;,
        &quot;description&quot;: &quot;Owner&quot;,
        &quot;can_read&quot;: true,
        &quot;can_write&quot;: 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.

huangapple
  • 本文由 发表于 2020年9月3日 22:28:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63725744.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定