第一種方法
SpringBoot中忽略實(shí)體類中的某個(gè)屬性不返回給前端的方法:使用Jackson的方式://第一種方式,使用@JsonIgnore注解標(biāo)注在屬性上,
//第一種方式,使用@JsonIgnore注解標(biāo)注在屬性上,忽略指定屬性
public class PropertyDTO {
private Integer disable;
private String placeholder;
//使用@JsonIgnore注解,忽略此屬性,前端不會(huì)拿到該屬性
@JsonIgnore
private String validate;
}
第二種方法
使用@JsonIgnoreProperties標(biāo)注在類上,可以忽略指定集合的屬性
//第二種方式,使用@JsonIgnoreProperties標(biāo)注在類上,可以忽略指定集合的屬性
@JsonIgnoreProperties({"validate"})
public class PropertyDTO {
private Integer disable;
private String placeholder;
private String validate;
}
注意:同時(shí)使用@JsonProperty和@JsonIgnore時(shí),可能會(huì)導(dǎo)致@JsonIgnore失效
第三種方法
使用fastjson時(shí):使用@JSONField(serialize = false)注解
public class PropertyDTO {
private Integer disable;
private String placeholder;
@JSONField(serialize = false)
private String validate;
}
第四種方法
加上 @JsonProperty(access = JsonProperty.Access.WRITE_ONLY) :前端就不能接收到文章來源:http://www.zghlxwxcb.cn/news/detail-548397.html
/**
* 密碼
*/
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private String password;
第五種方法
如果是null不返回,注解:@JsonInclude(value= JsonInclude.Include.NON_NULL) 返回的字段屬性為null 就不會(huì)展示給前端...可以放在類上,也可以放在字段上!文章來源地址http://www.zghlxwxcb.cn/news/detail-548397.html
@JsonInclude(value= JsonInclude.Include.NON_NULL)
public class PropertyDTO {
private Integer disable;
private String placeholder;
private String validate;
}
到了這里,關(guān)于java返回前端實(shí)體類json數(shù)據(jù)時(shí)如何忽略某個(gè)屬性的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!