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

相关推荐
iwS2o90XT3 分钟前
Java多线程编程:Thread与Runnable的并发控制
java·开发语言·python
wangchunting13 分钟前
spring-boot-starter-validation字段数据校验
java
阿Y加油吧13 分钟前
堆 / 优先队列专题二刷笔记:前 K 个高频元素 & 数据流的中位数
java·笔记·算法
凯尔萨厮17 分钟前
创建Springboot空项目
java·spring boot
深邃-38 分钟前
【Web安全】-Kali,Linux配置(2):Java环境配置,Python环境配置,Conda使用,PIP配置使用,SSH远程登录
java·linux·python·安全·web安全·网络安全·php
jjjava2.039 分钟前
Java多线程编程:从入门到实战
java·开发语言
Seven9740 分钟前
Tomcat 线程池的设计与实现:StandardThreadExecutor
java
爱笑的Sunday40 分钟前
Linux Java前后端项目 企业级0-1完整部署手册
java·linux·运维·服务器
xyx-3v41 分钟前
FreeRTOS队列通信
java·服务器·网络