[英]Jackson java object to json serialization is not including null fields
I have two java beans as below: 我有两个Java Bean,如下所示:
public class Class1 {
private String field1;
private Object field2;
//getter and setter
}
public class Class2 {
private Map<String,Object> field;
//getter and setter
}
When the object gets serialized to Json, it looks like this: 当对象被序列化为Json时,它看起来像这样:
Class1: When field2 is null 类别1:当field2为空时
{
field1:"value"
}
Class2: when value of map is null Class2:当map的值为null时
{
field:{"key":null}
}
My question is what is the difference between the two? 我的问题是两者之间有什么区别? why for Class1 it didn't include null field in json?
为什么对于Class1,它在json中不包含null字段? How do I include null field in json for Class1?
如何在Class1的json中包含空字段? I have tried the following but did't work:
我尝试了以下方法,但是没有用:
@JsonInclude(JsonInclude.Include.ALWAYS)
public class Class1 {
private String field1;
private Object field2;
//getter and setter
}
and even tried on field level: 甚至在现场层面尝试过:
public class Class1 {
private String field1;
@JsonInclude(JsonInclude.Include.ALWAYS)
private Object field2;
//getter and setter
}
I am using Jersey. 我正在使用泽西岛。
The following example is with jackson: 以下是杰克逊的示例:
ObjectMapper mapper = new ObjectMapper();
Class1 class1 = new Main().new Class1();
System.out.println(mapper.writeValueAsString(class1));
and the output is: 输出为:
{"field1":null,"field2":null}
{ “字段1”:空, “字段2”:空}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.