java返回前端實體類json數(shù)據(jù)時忽略某個屬性方法
第一種方法
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; }
總結(jié)
到此這篇關于java返回前端實體類json數(shù)據(jù)時忽略某個屬性的文章就介紹到這了,更多相關java忽略實體類某個屬性內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Spring MVC的優(yōu)點與核心接口_動力節(jié)點Java學院整理
這篇文章主要介紹了Spring MVC的優(yōu)點與核心接口,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08mybatis動態(tài)sql之Map參數(shù)的講解
今天小編就為大家分享一篇關于mybatis動態(tài)sql之Map參數(shù)的講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03