注解使用——@JsonIgnore

@JsonIgnore
作用:在json序列化时将java bean中的一些属性忽略掉,序列化和反序列化都受影响。

使用方法:一般标记在属性或者方法上,返回的json数据即不包含该属性。

场景模拟:

需要把一个List转换成json格式的数据传递给前台。但实体类中基本属性字段的值都存储在快照属性字段中。此时我可以在业务层中做处理,把快照属性字段的值赋给实体类中对应的基本属性字段。最后,我希望返回的json数据中不包含这两个快照字段,那么在实体类中快照属性上加注解@JsonIgnore,那么最后返回的json数据,将不会包含goodsInfo和extendsInfo两个属性值。

public class HistoryOrderBean {

//基本属性字段
private String insurantName;
private String insuranceName;
private String insurancePrice;
private String insurancePicture;
private String insuranceLimit;

//快照属性字段
@JsonIgnore
private String goodsInfo;      //快照基本信息
@JsonIgnore  
private String extendsInfo;    //快照扩展属性信息

}

4.注解失效:
如果注解失效,可能是因为你使用的是fastJson,尝试使用对应的注解来忽略字段,注解为:@JSONField(serialize = false),使用方法一样。

原文:https://blog.csdn.net/fakerswe/article/details/78626085

你可能感兴趣的:(注解)