SpringBoot中忽略实体类中的某个属性不返回给前端的方法(示例详解)
作者:ZmyCoder
本文介绍了在Spring Boot中使用Jackson和Fastjson忽略实体类属性不返回给前端的方法,在Jackson中,同时使用@JsonProperty和@JsonIgnore时,@JsonIgnore可能失效,Fastjson中可以使用@JSONField(serialize=false)来实现,本文结合实例代码介绍的非常详细,需要的朋友参考下吧
使用Jackson的方式:
//第一种方式,使用@JsonIgnore注解标注在属性上,忽略指定属性
public class PropertyDTO {
@JsonProperty("disable")
private Integer disable;
@JsonProperty("placeholder")
private String placeholder;
//使用@JsonIgnore注解,忽略此属性,前端不会拿到该属性
@JsonIgnore
private String validate;
}
//第二种方式,使用@JsonIgnoreProperties标注在类上,可以忽略指定集合的属性
@JsonIgnoreProperties({"validate"})
public class PropertyDTO {
@JsonProperty("disable")
private Integer disable;
@JsonProperty("placeholder")
private String placeholder;
private String validate;
}注意点:
public class PropertyDTO {
@JsonProperty("disable")
private Integer disable;
@JsonProperty("placeholder")
private String placeholder;
@JsonProperty("validate")
@JsonIgnore
private String validate;
}同时使用@JsonProperty和@JsonIgnore时,可能会导致@JsonIgnore失效,前端依旧拿到该属性。
使用fastjson时:
使用@JSONField(serialize = false)注解
到此这篇关于SpringBoot中忽略实体类中的某个属性不返回给前端的方法的文章就介绍到这了,更多相关SpringBoot某个属性不返回前端内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
