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