【发布时间】:2019-08-14 17:59:02
【问题描述】:
使用 json 保存和加载数据需要 json 的构造函数来加载对象,而我无法让 lombok 注释来处理它。我该怎么办?
这是我的班级在尝试使用注释来构造我的项目之前和之后的样子:
@Data
public class Item { //before
private int id;
private int amount;
public Item(@JsonProperty("id") int id, @JsonProperty("amount") int amount) {
this.id = id;
this.amount = amount;
}
}
@Data
@AllArgsConstructor
@NoArgsConstructor //I don't want this here as it could cause complications in other places. But json requires I have this...
public class Item { //after
private int id;
private int amount;
}
我不想使用 lombok 的 NoArgsConstructor 注释,因为我不想要此类的无参数构造函数。我意识到我可以这样做:
private Item() {
}
但希望有更好的方法...
【问题讨论】:
-
尝试使用
@AllArgsConstructor(suppressConstructorProperties = true) -
@YCF_L 该方法已弃用
-
试试
@AllArgsConstructor(onConstructor = @__(@JsonCreator)),另见Can't deserialize simplest Object with final field bu Jackson
标签: java json jackson annotations lombok