最近写项目,用到Jackson的一些注解,总结一下,帮助本身记忆。java
1.@JsonIgnore 和 @JsonIgnorePropertiesjson
两个注解能够对照比较后选择使用:spa
@JsonIgnoreclass
在json序列化时将java bean中的一些属性忽略掉,序列化和反序列化都受影响。序列化
private String name;
private Integer age;
@JsonIgnore
private String color;方法
运行方法在控制台打印总结
System.out.println(name+""+age+""+color+"");数据
将只显示name和age,color受注解的影响不进行显示。项目
使用方法:通常标记在属性或者方法上,返回的json数据即不包含该属性。word
@JsonIgnoreProperties
和 @JsonIgnore 的做用相同,都是告诉 Jackson 该忽略哪些属性,
不一样之处是 @JsonIgnoreProperties 是
类级别的,而且能够同时指定多个属性。
@JsonIgnoreProperties(value = {"age","color"})
public class
TestJackson{
private String id;
private String username;
private String password;
private Integer age;
private String color;
}
类中的age和color属性都将会在序列化和反序列化时被忽略掉。
2.@JsonProperty
它用于属性上,做用是
把属性的名称序列化成另一个名称
@JsonProperty("name")
private String username;
把username属性序列化为name
3.@JsonInclude(JsonInclude.Include.NON_NULL)
这个注解表示,若是
值为null,则不返回,还能够在类上添加这个注释,当实体类与json互相转换的时候,
属性值为null的不参与序列化。
@JsonInclude(JsonInclude.Include.NON_NULL)
public Vector children;
@JsonInclude(JsonInclude.Include.NON_NULL)
public class
TestJackson{
xxxxxx
}