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

使用Jackson的方式

java 复制代码
//第一种方式,使用@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;
}

注意点

java 复制代码
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)注解

相关推荐
catino4 分钟前
spring-事务@Transactional
java·数据库·spring
万年咸鱼16 分钟前
Java List 接口详解:从基础到实战
java·windows·list
Java的搬运工19 分钟前
Spring boot 事件监听
java·spring boot·spring·编程语言
tryxr23 分钟前
Chat2Excel 项目网关服务开发
java·gateway·网关模块开发
谢亮_vipxieliang23 分钟前
ValidX vs Apache Commons Validator:功能与性能对比
java·服务器·spring boot·后端·spring cloud·apache·hibernate
tryxr28 分钟前
Chat2Excel 项目通用服务开发
java·开发语言·excel·java项目开发
小裕哥略帅32 分钟前
Spring AOP 实现通用 Token 自动刷新重试工具包
java·后端·spring
Lenyiin36 分钟前
第6篇_Python高级语法与标准库精髓:从会用到达精通
java·python·html
YHHLAI39 分钟前
TypeScript 面试题:type 与 interface 的区别与相同点
java·ubuntu·typescript