1.响应json时排除对象中,属性值为Null,的属性。
在Bean类上加@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Video {
private Integer id;
private String title;
private String intro;
........
}
2.1 响应json时,指定属性名,响应。
在指定属性的 get方法上面使用@JsonProperty(" ") 注解
public class Video {
private Integer id;
private String title;
@JsonProperty("video_name")
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
........
}
2.2 在Json字符串作为请求参数时,要绑定到Bean对象属性上。
如果json中的字符串名和Bean对象中的属性名不一致可能就会导致绑定不上。
在Bean指定属性的 set方法上面使用@JsonProperty(" ") 注解
public class Video {
private Integer id;
private String title;
public String getTitle() {
return title;
}
@JsonProperty("video_name")
public void setTitle(String title) {
this.title = title;
}
........
}
2.直接在 Bean中指定属性上面使用@JsonProperty(" ") 注解,此时,不管是响应还是注入都可以使用指定名称。绑定或者响应
public class Video {
private Integer id;
@JsonProperty("video_name")
private String title;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
........
}
3.响应json时忽略某些属性 使用 @JsonIgnore注解
· 如果直接在title属性上使用,响应时Json字符串中就不会再有这个title属性。
· 但是:当在新增Video的时候Json字符串参数绑定VideoBean时,title属性也被忽略。导致绑定不上这个值。
· 解决办法就是,不在title属性上直接加这个注解,在title对应的get方法上面使用这个注解
public class Video {
private Integer id;
@JsonIgnore
private String title;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public class Video {
private Integer id;
private String title;
@JsonIgnore
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
3.响应json时指定日期格式
在日期属性上面加入注解
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = “yyyy-MM-dd”, timezone = “GMT+8”)
public class Video {
private Integer id;
private String title;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", timezone = "GMT+8")
private Date createdAt;
........
}