java返回前端实体类json数据时忽略某个属性方法
第一种方法
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; }
总结
到此这篇关于java返回前端实体类json数据时忽略某个属性的文章就介绍到这了,更多相关java忽略实体类某个属性内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Spring Cloud Gateway打造可扩展的微服务网关
微服务网关是一个位于客户端和后端微服务之间的服务器,用于处理所有与客户端的通信,Spring Cloud Gateway都是一个值得考虑的选择,它将帮助您更好地管理和保护您的微服务,感兴趣的朋友一起看看吧2023-11-11springBoot 之spring.factories扩展机制示例解析
这篇文章主要为大家介绍了springBoot 之spring.factories扩展机制示例解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2023-04-04
最新评论