SpringBoot中忽略实体类中的某个属性不返回给前端的方法

使用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)注解

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值