我刚刚熟悉杰克逊绑定。但是,当我测试 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);
}
}
Run Code Online (Sandbox Code Playgroud)
和 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;
}
}
Run Code Online (Sandbox Code Playgroud)
输出是
{"name":null,"age":null,"birth":null}
{"name":"ss","age":null,"birth":null}
Run Code Online (Sandbox Code Playgroud)
后一个应该排除空值,但事实并非如此。
但是,当我这样放置代码时。
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);
}
}
Run Code Online (Sandbox Code Playgroud)
它适用于下面的输出
{}
{"name":"ss"}
Run Code Online (Sandbox Code Playgroud)
这是正常现象还是只是某种错误?我想念什么吗?唯一的 maven 依赖项是 jackson-databind 2.7.4。欢迎任何讨论。谢谢!
ObjectMappers使用时请勿更改设置。一旦使用映射器,由于序列化器和反序列化器的缓存,并非所有设置都会生效。
配置实例一次,首次使用后请勿更改设置。这样做是为了线程安全和性能。
更新:无效链接替换为 archive.org 链接
| 归档时间: |
|
| 查看次数: |
11758 次 |
| 最近记录: |