@JsonIgnorePerperties
/**
注解可以用来抑制属性的序列化(在序列化期间),或忽略处理JSON属性的读取(在反序列化期间)、
例如:
阻止指定的字段被序列化或反序列化(在JSON的输出中不包括; 即使包括在字符串中也不会被设置)
@JsonIgnoreProperties({“internalId”, "secretKey"})
在JSON输入时忽略任何未知的属性文件且不具有异常:
@JsonIgnoreProperties(ignoreUnkown=true)
自2.0版本之后,此注解可用于类和属性。如果二者都使用了,真正的设置会结果所有的ignorals:
也就是你可以仅添加被忽略的属性,而不是移除或覆盖。因此你不能移除属性来忽略使用per-property
annotation
*/
@Target(ElementType.ANNOTATION_TYPE, ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotation
public @interface JsonIgnoreProperties{
/**
要忽略的属性
*/
public String[] value() default {};
/**
该属性定义了在反序列化期间是否忽略没有识别的的属性。
如果为true,没有识别的所有属性。也就是,没有setters和creators接受他们的
将会被忽略并且不产生警告(although handlers for unknown properties,
if any, will still be called)不产生异常。
*/
public boolean ignoreUnknow() default false;
}
举例
@JsonIgnoreProperties - one of the most common annotations in Jackson - is used to mark a property or a list properties to be ignored at the class level
Let's go over a quick example ignoring the property id from serialization
@JsonIgnoreProperties({"id"})
public class MyBean3 {
private Long id;
private String name;
}
And here's the test making sure the ignore happens:
@Test
public void test04() throws Exception {
MyBean3 myBean3 = new MyBean3();
myBean3.setId(1L);
myBean3.setName("dengyi");
ObjectMapper mapper = new ObjectMapper();
String s = mapper.writeValueAsString(myBean3);
Assert.assertThat(s, CoreMatchers.containsString("dengyi"));
Assert.assertThat(s, CoreMatchers.not("id"));
}