我有一个问题,将Json对象写入lombok对象。这里是我的Json对象:
```{"id": "1","name":"home","order": "1","isSubMenu": false,"path":"home","subMenu": null},{"id": "2","name":"user","order": "2","isSubMenu": false,"path":"usermanagement" ,"subMenu": [{"id": "21","name":"access","order": "1","isSubMenu": true,"path":"access" ,"subMenu": null},{"id": "22","name":"user","order": "2","isSubMenu": true,"path":"group" ,"subMenu": null},{"id": "23","name":"group","order": "3","isSubMenu": true,"path":"user" ,"subMenu": null}]}}```
我的Lombok岛:
@Value
@JsonDeserialize()
@JsonIgnoreProperties(ignoreUnknown = true)
public class Menu {
private String id;
private String name;
private Integer order;
private Boolean isSubMenu;
private String path;
private List<Menu> subMenuList;
}```
and my service:
```ObjectMapper mapper = new ObjectMapper();
Menu menu = mapper.convertValue(outPutObject, Menu.class);
最后,我的例外:com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of com.barman.framework.model.Menu (no Creators, like default constructor, exist): no String-argument constructor/factory method to deserialize from String value ('')at [Source: UNKNOWN; line: -1, column: -1]
1条答案
按热度按时间vjhs03f71#
当你用lomboks
@Value
注解你的类时,这意味着你定义你的类是不可变的,这意味着构造函数必须初始化所有成员(lombok会把它们装饰成final
)。Jackson默认反序列化器的工作原理是调用一个空构造函数,然后在每个与JSON中的字段匹配的成员上调用setter。如果没有默认的构造函数(你在lomboks@value
中不会有这个构造函数),那么它会抛出你看到的错误。你基本上有两个选择:
1.快速和肮脏:
更换
与
但是这会破坏类的设计,因为它不再是不可变的(因为
@Data
添加了setter)。您将需要评估这对于您的设计是否可接受。2.创建自定义反序列化器:
在菜单类中,将
@JsonDeserialize
更新为:然后定义你的Deserializer如下:
这个选项是更多的代码和faff,但你保持不变,这是所有酷孩子做的