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

相关推荐
云烟成雨TD1 天前
Spring AI Alibaba 1.x 系列【6】ReactAgent 同步执行 & 流式执行
java·人工智能·spring
于慨1 天前
Lambda 表达式、方法引用(Method Reference)语法
java·前端·servlet
swg3213211 天前
Spring Boot 3.X Oauth2 认证服务与资源服务
java·spring boot·后端
gelald1 天前
SpringBoot - 自动配置原理
java·spring boot·后端
殷紫川1 天前
深入理解 AQS:从架构到实现,解锁 Java 并发编程的核心密钥
java
一轮弯弯的明月1 天前
贝尔数求集合划分方案总数
java·笔记·蓝桥杯·学习心得
chenjingming6661 天前
jmeter线程组设置以及串行和并行设置
java·开发语言·jmeter
殷紫川1 天前
深入拆解 Java volatile:从内存屏障到无锁编程的实战指南
java
eddieHoo1 天前
查看 Tomcat 的堆内存参数
java·tomcat
那个失眠的夜1 天前
Mybatis延迟加载策略
xml·java·数据库·maven·mybatis