1、@JsonIgnoreProperties
类别:类注解
作用:json序列化时将bean中的一些属性忽略掉,序列化和反序列化都受影响
内部可以写多个属性
@JsonIgnoreProperties({"name"},{"age"})
allowGetters,allowSetters一起用用来控制字段忽视是在序列化还是反序列化
allowGetters=true:字段允许序列化,反序列的时候忽略该字段
allowSetters=true:字段允许反序列化,序列化的时候忽略该字段
ignoreUnknown=true:反序列化的时候忽视未知的字段,解决字段无法对应实体类会报错json解析异常
2、@JsonIgnore
类别:属性或方法上的注解(最好是属性上)
作用:用来完全忽略被注解的字段和方法对应的属性,序列化和反序列化都受影响
@JsonIgnore
private int age;
标记注释,指示基于内省的序列化和反序列化功能将忽略带注解的方法或字段。也就是说,它不应该被认为是“getter”、“setter”或“creator”(it should not be consider a "getter", "setter" or "creator".)
如果一个“getter”方法表示要序列化的属性(比如说,“getValue”表示要序列化的属性“value”),则该方法将被忽略,除非另一个注释定义了要使用的替代方法,否则不会输出此类属性。
此注释仅在方法对方法(或字段对字段)的基础上工作;一个方法或字段上的注释并不意味着忽略其他方法或字段。具体地说,标记“setter”候选者不会改变匹配“getter”方法的处理(反之亦然)。
没有显式定义“value”参数(默认为true):但参数可以显式定义。可以通过使用“false”参数明确定义一个JsonIgnore来覆盖现有JsonIgnore。
注释类似于javax.xml.bind.Annotation.XmlTransient
3、@JsonFormat
类别:用于属性或方法上(最好是属性上)
在需要查询出来的时间的数据库字段对应的实体类的属性上添加
@JsonFormat(pattern="yyyy-MM-dd",timezone = "GMT+8")
pattern: 需要转换的时间日期的格式
timezone:将时间设置为东八区,避免时间在转换中有误差
可以在属性对应的get方法上,两种方式没有区别
4、@JsonSerialize
用于属性或getter方法,在序列化时嵌入自定义代码
需要使用 using 属性指定处理参数的类,该类需要继承 JsonSerializer 类,并重写 serialize()。
若使用了 Lombok 需要自己定义相应的 get 方法。
以将日期格式化为yy-mm-dd为例。
@JsonSerialize(using = CustomDateSerialize.class)
public Date getDate() {
return date;
}
/**
* @author orchid
* @date 2021-08-03 14:14
* @description 日期序列化工具, 直接将时间类型的转为yyyy-MM-dd类型的数据
*/
public class CustomDateSerialize extends JsonSerializer<Date> {
// 定义日期格式
private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
@Override
public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
jsonGenerator.writeString(simpleDateFormat.format(date));
}
}
5、@JsonDeserialize
用于属性或者setter方法上,用于在反序列化时可以嵌入我们自定义的代码,类似于上面的@JsonSerialize
需要使用 using 属性指定处理参数的类,该类需要继承 JsonSerializer 类,并重写 serialize()。
若使用了 Lombok 需要自己定义相应的 get 方法。
在前端性别显示“男 / 女”;数据库中存储的是“1 / 0”,对应的 Pojo 也是使用的 Integer 类型
@Data
public class Person implements Serializable {
private static final long serialVersionUID = 4346092911489022673L;
/**
* 1 男,0 女
*/
private Integer gender;
@JsonDeserialize(using = GenderJsonDeserializer.class)
public void setGender(Integer gender) {
this.gender = gender;
}
@JsonSerialize(using = GenderJsonSerializer.class)
public Integer getGender() {
return gender;
}
}
JsonDeserializer 作用是:处理参数,按照规则封装到指定的属性中
通过 jsonParser.getText() 获取参数
/**
* @author orchid
* @date 2021-08-03 14:43
* @description
*/
public class GenderJsonDeserializer extends JsonDeserializer {
@Override
public Object deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
if (ObjectUtils.isEmpty(jsonParser)) {
return null;
}
int gender = 0;
switch (jsonParser.getText()) { // 通过getText获取参数
case "男":
gender = 1;
break;
case "女":
break;
default:
throw new RuntimeException("传入的性别为非法字符");
}
return gender;
}
}
JsonSerializer 作用是:处理属性,按照规则封装到指定的参数中,通过value获取属性
通过 jsonGenerator.writeXxx() 写出参数
/**
* @author orchid
* @date 2021-08-03 14:43
* @description
*/
public class GenderJsonSerializer extends JsonSerializer {
@Override
public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
if (o.equals(1)) {
jsonGenerator.writeString("男"); // 写出参数
} else if (o.equals(0)) {
jsonGenerator.writeString("女"); // 写出参数
}
}
}
6、@Transient
属性不与数据库表的字段映射——该字段在数据库中不存在,而需要使用。标示为@Transient
该属性不存储到数据库,不修改已存在数据库中数据的数据结构
//表示该字段在数据库表中不存在
@Transient
public int getAge() {
return 1+1;
}
7、@JsonIgnoreType
类别:类注解
作用:类在序列化和反序列化的时候被忽略
8、@JsonProperty
作用:指定某个属性和json映射的名称
1.前端传过来的参数,想用其他属性名接收,可以在属性上加此注解
2.后端返回给前端数据时,前端要求指定key,可在属性上加此注解,就会以注解中的value返回
@JsonProperty是@JsonGetter与@JsonSetter的合体版
public class User {
@JsonProperty("user_name")
private String userName;
}
返回的json
{“user_name”:”xxx”},
只在序列化情况下生效的注解
1、@JsonPropertyOrder
在将 java pojo 对象序列化成为 json 字符串时,使用 @JsonPropertyOrder 可以指定属性在 json 字符串中的顺序。
2、@JsonInclude
在将 java pojo 对象序列化成为 json 字符串时,使用 @JsonInclude 注解可以控制在哪些情况下才将被注解的属性转换成 json
ALWAYS :默认策略,任何情况下都序列化该字段
NON_NULL:注解的字段为null不序列化
NON_ABSENT:注解的字段为null的时候不序列化
This option is mostly used to work with "Optional"s (Java 8, Guava)
NON_EMPTY :注解的字段为null或为空不序列化
NON_DEFAULT:字段是默认值的话就不序列化
CUSTOM:自定义排除序列化规则
过滤器对象的 equals() 方法被调用并带有要序列化的值; 如果返回真值则排除(即过滤掉);
USE_DEFAULTS:
伪值用于指示更高级别的默认值是有意义的,以避免覆盖包含值。 例如,如果返回一个属性,这将使用包含属性的类的默认值,如果有的话; 如果没有为此定义,则全局序列化包含详细信息。
4、@JsonGetter
反序列化时指定字段名
@JsonGetter("username")
public String getName() {
return name;
}
@JsonAnySetter
反序列化时实体对象不存在对应的属性时加上该 注解,不存在的属性都会放到Map中去
@Data
public class People {
private String username;
private Map<String, String> properties;
@JsonAnyGetter
public Map<String, String> getProperties() {
return properties;
}
}
在反序列化情况下生效的注解
1、@JsonSetter
@JsonSetter 标注于 setter 方法上,类似 @JsonProperty ,也可以解决 json 键名称和 java pojo 字段名称不匹配的问题。
前端传过来json转换为java对象时,json的key与实体类的属性不一致 ,用@JsonSetter注解做转换
@JsonSetter("userId")
public void settId(String id) {
this.id = id;
}
2、@JsonAnyGetter
序列化时Map加上该属性可以将map中的属性序列化为指定字符串
@Data
public class People {
private String username;
private Map<String, String> properties;
@JsonAnySetter
public void setProperties(Map<String, String> properties) {
this.properties = properties;
}
}
参考:
https://www.cnblogs.com/wdss/p/11832315.html
https://blog.csdn.net/weixin_44130081/article/details/89671016
https://blog.csdn.net/yage124/article/details/107321339/
https://www.cnblogs.com/mracale/p/9828346.html
https://blog.csdn.net/qq_29645505/article/details/89007323
https://blog.csdn.net/weixin_44130081/article/details/89678450
https://blog.csdn.net/reee112/article/details/83345629
https://blog.csdn.net/yaomingyang/article/details/106069715