我刚刚开始熟悉Jackson绑定,但是,当我测试setSerializationInclusion(JsonInclude.Include.NON_NULL)时,我发现它有时候不工作。
这是我的代码
package com.blithe.main;
import com.blithe.model.Student;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Jackson_2_NullValue {
public static void main(String[] args) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
Student s = new Student();
String stundetString = mapper.writeValueAsString(s);
System.out.println(stundetString);
// exclude null fields
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
s.setName("ss");
stundetString = mapper.writeValueAsString(s);
System.out.println(stundetString);
}
}
和POJO
package com.blithe.model;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
// @JsonIgnoreProperties(ignoreUnknown = true)
// exclude null fields for the whole class
// @JsonInclude(Include.NON_NULL)
public class Student {
// exclude the field whe it's empty ("")
// @JsonInclude(value=Include.NON_EMPTY)
private String name;
private Integer age;
private Date birth;
// Jackson ignores it
@JsonIgnore
private String nickName;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
}
输出为
{"name":null,"age":null,"birth":null}
{"name":"ss","age":null,"birth":null}
后一个应该排除空值,但它没有。
但是,当我这样编写代码时。
package com.blithe.main;
import com.blithe.model.Student;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Jackson_2_NullValue {
public static void main(String[] args) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
Student s = new Student();
String stundetString = mapper.writeValueAsString(s);
System.out.println(stundetString);
// exclude null fields
// mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
s.setName("ss");
stundetString = mapper.writeValueAsString(s);
System.out.println(stundetString);
}
}
它使用下面的输出
{}
{"name":"ss"}
这是正常的还是只是某种bug?我错过了什么吗?唯一的maven依赖是jackson-databind 2.7.4。欢迎讨论。谢谢!
2条答案
按热度按时间mwyxok5s1#
使用时不要更改
ObjectMappers
设置。Map器一旦使用,由于序列化程序与反序列化程序的缓存,并非所有设置都生效。配置一次示例,并且在第一次使用后不更改设置。这样做是为了线程安全和性能。
更新:用www.example.com的链接替换失效链接archive.org
u3r8eeie2#
因此,关键是如果您在多个位置使用ObjectMapper,请尽量不要反复创建对象。它采用首次初始化的配置。
如果你继续在全球范围内改变,它将不会起作用。