指定字段不返回:@JsonIgnore
封装的类
@Data
public class User {
private int id;
private String username;
/**
* @JsonIgnore
* 此注解是类注解,作用是json在序列化时将java bean 中的一些属性忽略掉,序列化和反序列化都受影响
* 生成json时不生成pwd属性
*/
@JsonIgnore
private String pwd;
public User(){}
public User(int id,String username,String pwd){
this.id=id;
this.username=username;
this.pwd=pwd;
}
}
调用api接口时返回的json数据:
指定日期格式
空字段不返回
指定别名
封装类:
@Data
public class Video implements Serializable {
private int id;
private String title;
@JsonInclude(JsonInclude.Include.NON_NULL)
private String summary;
private int price;
@JsonInclude(JsonInclude.Include.NON_NULL)
private String coverImg;
//返回指定的别名
@JsonProperty("create_time")
// 返回指定的时间格式
@JsonFormat(pattern ="yyyy-MM-dd hh:mm:ss",locale="zh",timezone = "GMT+8")
private Date createTime;
@JsonInclude(JsonInclude.Include.NON_NULL)
private List<Chapter> chapterList;
public Video(){} //无参构造
public Video(int id,String title){
this.id =id;
this.title =title;
this.createTime =new Date();
}
}
调用接口显示: