1.@JsonInclude的使用
在对象序列化成为 json 字符串时,使用 @JsonInclude 注解可以控制在哪些情况下才将被注解的属性转换成 json,例如只有属性NON_NULL 时。
默认JsonInclude.Include.ALWAYS
ALWAYS,
NON_NULL,
NON_ABSENT,
NON_EMPTY,
NON_DEFAULT,
CUSTOM,
USE_DEFAULTS;
2.@JsonIgnore和@JsonIgnoreProperties
@JsonIgnore(作用在属性或方法): 属性使用的注解,用于忽略指定属性。
@JsonIgnoreProperties(作用在类): 实体类使用的注解,用于序列化的时候忽略指定的一系列属性,或者反序列化的时候忽略未知的属性(没有getter/setter的属性)。
序列化的时候,@JsonIgnoreProperties({“prop1”, “prop2”}),忽略列表中的属性。
反序列化的时候,@JsonIgnoreProperties(ignoreUnknown=true),忽略没有get/set的属性
3.@JsonIgnoreType
@JsonIgnoreType:实体类使用的注解,表示该类被忽略
4.@JsonProperty
@JsonProperty:属性使用的注解,用来表示外部属性名字,就是使用别名序列化,而不是对象的名字。
5. @JsonPropertyOrder
在将 实体类序列化成为 json 字符串时,使用 @JsonPropertyOrder 可以指定属性在 json 字符串中的顺序
6.@JsonSetter
@JsonSetter 标注于 setter 方法上,类似 @JsonProperty ,也可以解决 json 键名称和实体类字段名称不匹配的问题
7.@JsonSerialize
@JsonSerialize(using = custom.class)
public class customSerializer extends JsonSerializer<T> {
T:为所注解的字段类型
@Override
public void serialize(T t, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
jsonGenerator.writeString(String var1)
}
}