Jackson常用注解介绍
1.排除属性
- @JsonIgnoreProperties
此注解是类注解,作用是json序列化时将java bean中的一些属性忽略掉,序列化和反序列化都受影响。
- @JsonIgnore
此注解用于属性或者方法上(最好是属性上),作用和上面的@JsonIgnoreProperties一样。
2.属性别名
- @JsonProperty
此注解用于序列化/反序列化都有效;
3.属性排序
- @JsonPropertyOrder
注释在类声明中
4.JSON格式化
- @JsonFormat
此注解用于属性或者方法上(最好是属性上),可以方便的把Date类型直接转化为我们想要的模式,比如@JsonFormat(pattern= "yyyy-MM-dd HH-mm-ss")
5.属性格式转换
- @JsonSerialize 序列化
此注解用于属性或者getter方法上,用于在序列化时嵌入我们自定义的代码,比如序列化一个double时在其后面限制两位小数点。
- @JsonDeserialize 反序列化
此注解用于属性或者setter方法上,用于在反序列化时可以嵌入我们自定义的代码,类似于上面的@JsonSerialize
6.父/子关联
- @JsonManagedReference,放在父亲类中;
- @JsonBackReference,放在孩子类中;
7.去掉包装
- @JsonUnwrapped
Ability to map JSON like { "name" : "home", "latitude" : 127, "longitude" : 345 } to classes defined as: class Place { public String name;
@JsonUnwrapped public Location location; }
class Location { public int latitude, longitude; }
完整例子
//表示序列化时忽略的属性
@JsonIgnoreProperties(value = { "word" })
public class Person {
privateString name;
private intage;
privateboolean sex;
private Datebirthday;
privateString word;
privatedouble salary;
publicString getName() {
returnname;
}
public voidsetName(String name) {
this.name =name;
}
public intgetAge() {
returnage;
}
public voidsetAge(int age) {
this.age =age;
}
publicboolean isSex() {
returnsex;
}
public voidsetSex(boolean sex) {
this.sex =sex;
}
public DategetBirthday() {
returnbirthday;
}
//反序列化一个固定格式的Date
@JsonDeserialize(using = CustomDateDeserialize.class)
public voidsetBirthday(Date birthday) {
this.birthday = birthday;
}
publicString getWord() {
returnword;
}
public voidsetWord(String word) {
this.word =word;
}
//序列化指定格式的double格式
@JsonSerialize(using = CustomDoubleSerialize.class)
publicdouble getSalary() {
returnsalary;
}
public voidsetSalary(double salary) {
this.salary= salary;
}
publicPerson(String name, int age) {
this.name =name;
this.age =age;
}
publicPerson(String name, int age, boolean sex, Date birthday,
String word,double salary) {
super();
this.name =name;
this.age =age;
this.sex =sex;
this.birthday = birthday;
this.word =word;
this.salary= salary;
}
publicPerson() {
- Jackson常用注解介绍
- Jackson常用注解介绍
- Jackson常用注解介绍
- Jackson常用注解介绍
- Jackson常用注解介绍
- Jackson常用注解介绍
- spring中常用的注解介绍
- SpringMVC Controller介绍及常用注解
- SpringWeb项目常用注解简单介绍
- SpringMVC Controller介绍及常用注解
- Jackson常用注解
- SpringMVC Controller介绍及常用注解
- Spring常用注解介绍【经典总结】
- TestNG-常用注解介绍
- springboot常用注解介绍
- SpringMVC Controller介绍及常用注解
- sg-uap常用注解介绍
- Spring常用注解介绍【经典总结】
- [转]SpringMVC Controller介绍及常用注解
- SpringMVC Controller介绍及常用注解