第一种方法
SpringBoot中忽略实体类中的某个属性不返回给前端的方法:使用Jackson的方式://第一种方式,使用@JsonIgnore注解标注在属性上,
//第一种方式,使用@JsonIgnore注解标注在属性上,忽略指定属性
public class PropertyDTO {
private Integer disable;
private String placeholder;
//使用@JsonIgnore注解,忽略此属性,前端不会拿到该属性
@JsonIgnore
private String validate;
}第二种方法
使用@JsonIgnoreProperties标注在类上,可以忽略指定集合的属性
//第二种方式,使用@JsonIgnoreProperties标注在类上,可以忽略指定集合的属性
@JsonIgnoreProperties({"validate"})
public class PropertyDTO {
private Integer disable;
private String placeholder;
private String validate;
}注意:同时使用@JsonProperty和@JsonIgnore时,可能会导致@JsonIgnore失效
第三种方法
使用fastjson时:使用@JSONField(serialize = false)注解
public class PropertyDTO {
private Integer disable;
private String placeholder;
@JSONField(serialize = false)
private String validate;
}第四种方法
加上 @JsonProperty(access = JsonProperty.Access.WRITE_ONLY) :前端就不能接收到
/**
* 密码
*/
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private String password;第五种方法
如果是null不返回,注解:@JsonInclude(value= JsonInclude.Include.NON_NULL) 返回的字段属性为null 就不会展示给前端...可以放在类上,也可以放在字段上!
@JsonInclude(value= JsonInclude.Include.NON_NULL)
public class PropertyDTO {
private Integer disable;
private String placeholder;
private String validate;
}
文章介绍了在SpringBoot中防止实体类属性被序列化并返回给前端的五种策略,包括使用@JsonIgnore注解单个属性,@JsonIgnoreProperties注解整个类,fastjson的@JSONField(serialize=false),@JsonProperty(access=JsonProperty.Access.WRITE_ONLY)以及@JsonInclude(value=JsonInclude.Include.NON_NULL)来处理null值。
1746

被折叠的 条评论
为什么被折叠?



