杰克逊:将null字符串反序列化为空字符串
问题描述:
我有以下类,由Jackson(简化版)映射:
I have the following class, that is mapped by Jackson (simplified version):
public class POI {
@JsonProperty("name")
private String name;
}
在某些情况下,服务器返回name: null
然后我想将name设置为空Java String。
In some cases the server returns "name": null
and I would like to then set name to empty Java String.
是否有任何Jackson注释或我应该检查内部的null我的getter并返回空字符串,如果属性是 null
?
Is there any Jackson annotation or should I just check for the null inside my getter and return empty string if the property is null
?
答
您可以在默认构造函数中设置它,也可以在声明中设置它:
You can either set it in the default constructor, or on declaration:
public class POI {
@JsonProperty("name")
private String name;
public POI() {
name = "";
}
}
OR
public class POI {
@JsonProperty("name")
private String name = "";
}