目录
1. 过滤 json 数据
2. 格式化 json 数据
3. 扁平化对象
1. 过滤 json 数据
@JsonIgnoreProperties
作用在类上用于过滤掉特定字段不返回或者不解析。
//生成json时将userRoles属性过滤
@JsonIgnoreProperties({"userRoles"})
public class User {
private String userName;private String fullName;private String password;@JsonIgnoreprivate List<UserRole> userRoles = new ArrayList<>();
}
@JsonIgnore
一般用于类的属性上,作用和上面的@JsonIgnoreProperties
一样。
public class User {
private String userName;private String fullName;private String password;//生成json时将userRoles属性过滤@JsonIgnoreprivate List<UserRole> userRoles = new ArrayList<>();
}
2. 格式化 json 数据
@JsonFormat
一般用来格式化 json 数据。:
比如:
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", timezone="GMT")
private Date date;
3. 扁平化对象
@Getter
@Setter
@ToString
public class Account {@JsonUnwrappedprivate Location location;@JsonUnwrappedprivate PersonInfo personInfo;
@Getter@Setter@ToStringpublic static class Location {private String provinceName;private String countyName;}@Getter@Setter@ToStringpublic static class PersonInfo {private String userName;private String fullName;}
}
未扁平化之前:
{"location": {"provinceName":"河北","countyName":"廊坊"},"personInfo": {"userName": "ww1234","fullName": "shanghai"}
}
使用@JsonUnwrapped
扁平对象之后:
@Getter
@Setter
@ToString
public class Account {@JsonUnwrappedprivate Location location;@JsonUnwrappedprivate PersonInfo personInfo;......
}
{"provinceName":"河北","countyName":"廊坊","userName": "ww1234","fullName": "shanghai"
}