Jackson忽略序列化中的字段
Contents
1. 概述
本教程将展示在使用 Jackson 2.x将对象序列化为 JSON 时如何忽略某些字段。 当 Jackson 的默认值不够时,这非常有用,我们需要准确控制序列化为 JSON 的内容——并且有几种方法可以忽略属性。
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 mixins。 首先,我们为我们想要忽略的类型定义一个 MixIn 并使用*@JsonIgnoreType*来注解它:
@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);
现在我们可以序列化对象并确保JSON 输出中不存在intValue字段:
@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);
}