1、@JsonIgnore
作用在字段或方法上,用来完全忽略被注解的字段和方法对应的属性.
@JsonIgnore
private String name;
2、@JsonProperty
作用在字段或方法上,用来对属性的序列化/反序列化,可以用来避免遗漏属性,同时提供对属性名称重命名
@JsonProperty("userName")
private String name;
3、@JsonIgnoreProperties
作用在类上,用来说明有些属性在序列化/反序列化时需要忽略掉
注明忽略的属性列表
@JsonIgnoreProperties({"name","age"})
注明过滤掉未知的属性
@JsonIgnoreProperties(ignoreUnknown=true)
4、@JsonUnwrapped
作用在属性字段或方法上,用来将子JSON对象的属性添加到封闭的JSON对象,将对象扁平
@JsonUnwrapped
private UserInfo userInfo;
5、@JsonIdentityInfo
2.0+版本新注解,作用于类或属性上,用来在序列化/反序列化时为该对象或字段添加一个对象识别码,通常是用来解决循环嵌套的问题
@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id")
6、@JsonNaming
2.1+版本的注解,作用于类或方法,注意这个注解是在jackson-databind包中而不是在jackson-annotations包里,可以定制属性命名策略
@JsonNaming(value = PropertyNamingStrategy.KebabCaseStrategy.class)
7、@JsonTypeInfo
作用于类/接口,被用来开启多态类型处理,对基类/接口和子类/实现类都有效
JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "className")
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "resourceType")
8、@JsonSubTypes
作用于类/接口,用来列出给定类的子类,只有当子类类型无法被检测到时才会使用它,一般是配合@JsonTypeInfo在基类上使用
@JsonSubTypes({@JsonSubTypes.Type(value = xxx.class, name = "xxx"),@JsonSubTypes.Type(value = yyy.class, name = "yyy")})
9、@JsonTypeName
作用于子类,用来为多态子类指定类型标识符的值
@JsonTypeName(value = "CAR")
10、@JsonSerialize和@JsonDeserialize
作用于方法和字段上,通过 using(JsonSerializer)和using(JsonDeserializer)来指定序列化和反序列化的实现,通常我们在需要自定义序列化和反序列化时会用到,
@JsonDeserialize(using= DateJsonDeserializer.class)
@JsonSerialize(using= DateJsonSerializer.class)
private Date createDate;
public class DateJsonSerializer extends JsonSerializer<Date> {
public static final SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Override
public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
jsonGenerator.writeString(format.format(date));
}
}
public class DateJsonDeserializer extends JsonDeserializer<Date> {
public static final SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Override
public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
try
{
if(jsonParser != null && StringUtils.isNotEmpty(jsonParser.getText())){
return format.parse(jsonParser.getText());
}else {
return null;
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
throw new RuntimeException(e);
}
}
}
11、@JsonPropertyOrder
作用在类上,被用来指明当序列化时需要对属性做排序,可以自定义,也可以采用字母拼音顺序排序
@JsonPropertyOrder(value = {"age","name"})
@JsonPropertyOrder(alphabetic=true)
12、@JsonIgnoreType
作用于类,表示被注解该类型的属性将不会被序列化和反序列化
…
还有很多例如:@JsonView、@JsonFilter、@JsonAnySetter、@JsonCreator、@JacksonInject、@JsonPOJOBuilder
文章仅作为个人学习整理