@JsonIgnore

2025-11-18 02:40:16

JsonIgnore

/**

标记注解声明了标注的方法或字段会被忽略基于自省的序列化和反序列化。

这是,it should not be consider a "getter", "setter", 或"creator"

额外的,Jackson1.9之后,如果该注解仅与一个属性关联,将会使所有的属性被忽略:也就是,如果setter有这个注解getter没有这个注解,

getter也会被忽略。如果仍需要不同的访问者使用不同的注解;如果仅"getter"被忽略,别的访问都(setter or field)就该显示的标注

(usually {@link JsonProperty}来阻止被忽略

例如:一个"getter"方法将会代表一个属性(像,"getValue"代表一个属性"value")来序列化,

将会被忽略,no such property would be output unless another annotation defines alternative method to use.

在1.9版本之前,此注解仅在method-by-method(或field-by-field)工作;

一个方法或一个字段上的注解不会隐式的忽略其他的方法或字段。然而,在1.9版本之上,多个不同的访问者关联的注解(getter, setter, field, constructor parameter)

of 一个逻辑上的属性被关联起来了;代表着在以上访问者的一个标注了注解,可以影响到他的所有的访问者(如果getter或field没有另外的别的声明)

Annotation is usually used just a like a marker annotation, that is,

没有显示的定义'value'参数(默认值是true):但参数可以显式的指定。这个可显示的覆盖一个已经存在的JsonIgnore通过

显示的定义一个具有"false"参数的JsonIgnore注解

*/

@Target({ElementType.ANNOTATION_TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.FIELD})

@Retention(RetentionPolicy.RUNTIME)

@JacksonAnnotation

public @interface JsonIgnore

{

/**

可选的参数定义此注解是否被激活。

可以使用值'false'如果出于覆盖的目的(通常不需要);

通常需要的是使用"mix-in注解",默认值是“true”是正好的

可以缺失

*/

boolean value() default true;

}

举例

The @JsonIgnore annotation is used to mark a property to be ignored at the field level.

Let's use @JsonIgnore to ignore the property id from serialization.

public class BeanWithIgnore {

@JsonIgnore

public int id;

public String name;

}

And the test making sure that _id _was successfully ignored:

@Test

public void test05() throws Exception {

BeanWithIgnore bean = new BeanWithIgnore(1, "dengyi");

ObjectMapper mapper = new ObjectMapper();

String s = mapper.writeValueAsString(bean);

Assert.assertThat(s, CoreMatchers.containsString("dengyi"));

Assert.assertThat(s, CoreMatchers.not(CoreMatchers.containsString("id")));

}

苹果耳机保修期全解析:政策、查询与维修流程详解
找人做网站,找人做网站