Jackson 序列化时忽略属性
最后更新:2024年5月11日
1. 概述
本教程将展示如何在使用 Jackson 2.x 时忽略在将对象序列化为 JSON 时特定的字段。
当 Jackson 的默认设置不够用,并且我们需要精确控制序列化到 JSON 的内容时,这非常有用——并且有几种方法可以忽略属性。
要深入了解并学习我们可以用 Jackson 做到的其他很酷的事情,请访问 主要的 Jackson 教程。
更多阅读
2. 在类级别忽略字段
我们可以使用 @JsonIgnoreProperties 注解并按名称指定字段在类级别忽略特定字段
@JsonIgnoreProperties(value = { "intValue" })
public class MyDto {
private String stringValue;
private int intValue;
private boolean booleanValue;
public MyDto() {
super();
}
// standard setters and getters are not shown
}
现在我们可以测试,在对象写入 JSON 后,该字段确实不在输出中
@Test
public void givenFieldIsIgnoredByName_whenDtoIsSerialized_thenCorrect()
throws JsonParseException, IOException {
ObjectMapper mapper = new ObjectMapper();
MyDto dtoObject = new MyDto();
String dtoAsString = mapper.writeValueAsString(dtoObject);
assertThat(dtoAsString, not(containsString("intValue")));
}
3. 在字段级别忽略字段
我们也可以通过 直接在字段上使用 @JsonIgnore 注解直接忽略一个字段
public class MyDto {
private String stringValue;
@JsonIgnore
private int intValue;
private boolean booleanValue;
public MyDto() {
super();
}
// standard setters and getters are not shown
}
现在我们可以测试 intValue 字段确实不在序列化的 JSON 输出中
@Test
public void givenFieldIsIgnoredDirectly_whenDtoIsSerialized_thenCorrect()
throws JsonParseException, IOException {
ObjectMapper mapper = new ObjectMapper();
MyDto dtoObject = new MyDto();
String dtoAsString = mapper.writeValueAsString(dtoObject);
assertThat(dtoAsString, not(containsString("intValue")));
}
4. 忽略指定类型的全部字段
最后,我们可以使用 @JsonIgnoreType 注解忽略指定类型的全部字段。 如果我们控制该类型,那么我们可以直接注释该类
@JsonIgnoreType
public class SomeType { ... }
然而,通常我们无法控制类本身。在这种情况下,我们可以很好地利用 Jackson mixin。
首先,我们为我们想要忽略的类型定义一个 MixIn,并使用 @JsonIgnoreType 注解该 MixIn
@JsonIgnoreType
public class MyMixInForIgnoreType {}
然后,我们注册该 mixin 以在序列化期间替换(并忽略)所有 String[] 类型
mapper.addMixInAnnotations(String[].class, MyMixInForIgnoreType.class);
此时,所有 String 数组都将被忽略,而不是序列化到 JSON
@Test
public final void givenFieldTypeIsIgnored_whenDtoIsSerialized_thenCorrect()
throws JsonParseException, IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.addMixIn(String[].class, MyMixInForIgnoreType.class);
MyDtoWithSpecialField dtoObject = new MyDtoWithSpecialField();
dtoObject.setBooleanValue(true);
String dtoAsString = mapper.writeValueAsString(dtoObject);
assertThat(dtoAsString, containsString("intValue"));
assertThat(dtoAsString, containsString("booleanValue"));
assertThat(dtoAsString, not(containsString("stringValue")));
}
这是我们的 DTO
public class MyDtoWithSpecialField {
private String[] stringValue;
private int intValue;
private boolean booleanValue;
}
注意:从版本 2.5 开始,似乎我们无法使用此方法忽略原始数据类型,但我们可以将其用于自定义数据类型和数组。
5. 使用过滤器忽略字段
最后,我们还可以使用过滤器来忽略 Jackson 中的特定字段 。
首先,我们需要在 Java 对象上定义过滤器
@JsonFilter("myFilter")
public class MyDtoWithFilter { ... }
然后,我们定义一个简单的过滤器,该过滤器将忽略 intValue 字段
SimpleBeanPropertyFilter theFilter = SimpleBeanPropertyFilter
.serializeAllExcept("intValue");
FilterProvider filters = new SimpleFilterProvider()
.addFilter("myFilter", theFilter);
现在我们可以序列化对象,并确保 intValue 字段不在 JSON 输出中
@Test
public final void givenTypeHasFilterThatIgnoresFieldByName_whenDtoIsSerialized_thenCorrect()
throws JsonParseException, IOException {
ObjectMapper mapper = new ObjectMapper();
SimpleBeanPropertyFilter theFilter = SimpleBeanPropertyFilter
.serializeAllExcept("intValue");
FilterProvider filters = new SimpleFilterProvider()
.addFilter("myFilter", theFilter);
MyDtoWithFilter dtoObject = new MyDtoWithFilter();
String dtoAsString = mapper.writer(filters).writeValueAsString(dtoObject);
assertThat(dtoAsString, not(containsString("intValue")));
assertThat(dtoAsString, containsString("booleanValue"));
assertThat(dtoAsString, containsString("stringValue"));
System.out.println(dtoAsString);
}
6. 结论
本文说明了如何在序列化时忽略字段。我们首先按名称然后直接执行此操作,最后使用 MixIns 忽略整个 java 类型,并使用过滤器更精细地控制输出。
支持本文的代码可在 GitHub 上获取。 一旦你以 Baeldung Pro 会员 身份登录,就开始学习并在项目上进行编码。















