@JsonInclude、@JsonIgnore、@JsonFormat、@JsonSerialize、@JsonIgnoreProperties、@JsonIgnoreType

一、@JsonIgnore:

1、作用:

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

一般标记在属性或者方法上,在返回的json数据就不包含该属性

2、场景模拟:

将一个User序列化成Json数据并返回给前台,当我们在User的password和email属性上添加@JsonIgnore注解时,即使后台给这两个属性赋值了,返回前台的Json也不包含它们。

点击查看代码

public class User {

private String name;

private String age;

private String sex;

@JsonIgnore //添加JsonIgnore注解,返回时被忽略

private String password;

@JsonIgnore //添加JsonIgnore注解,返回时被忽略

private String email;

public User() {}

}

3、@JsonIgnore注解失效

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

补充:

jackSon中@JsonIgnore, @JsonIgnoreProperties and @JsonIgnoreType注解使用详解

@JsonIgnore

这个注解是用在字段上,get或者set方法上,效果都是一样的,用来在实体类序列化和反序列化的时候忽略该字段字段。

@JsonIgnoreProperties

这个注解和@JsonIgnore注解功能是一样的,区别就是这个注解是用在类上面的,在需要的注解比较多的情况下,用来一次性定义忽略的字段如:

@JsonIgnoreProperties({“username”,“password”})

@JsonIgnoreType

这个注解是用在类上面的表明这个类在序列化和反序列化的时候被忽略

@JsonSerialize

@JsonSerialize 作用是json序列化时按照自己定义的格式方法执行。

比如解决前端显示和后台存储数据单位不一致的问题。在返回对象时,进行自定义数据格式转换。

import com.fasterxml.jackson.core.JsonGenerator;

import com.fasterxml.jackson.databind.JsonSerializer;

import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;

import java.util.Date;

/**

@program: sell

@description: 转换Json序列化工具

**/

public class Date2LongSerializer extends JsonSerializer {

@Override

public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {

jsonGenerator.writeNumber(date.getTime() / 1000);

}

}

public class Person implements Serializable {

private String name;

@JsonIgnore

private String sex;

@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")

private Date birday;

@JsonSerialize(using=DoubleFormat.class)

private Double weight;

@JsonSerialize(using = Date2LongSerializer.class)

private Date createTime;

public String getName() {

return name;

}

}

结尾

大家平时在处理实体类序列化和反序列的时候可以根据不同的需求和场景来搭配使用这些注解可以达到事半功倍的效果,因为我以前经常写的时候就会因为不同的情况而可能创建好几个实体类,或者对实体类要进行很多处理步骤

注解@JsonIgnore注意:

注解@JsonIgnore的作用是“在实体类向前台返回数据时用来忽略不想传递给前台的属性或接口。”

例如:User实体中有一个字段password,当我们用User实体作为输出类给前端返回用户信息的时候,并不希望将password值也一并返回。这个时候就可以在password属性上加上注解JsonIgnore或者,可以在User类上加上注解@JsonIgnoreProperties(value = "{password}")

但是,要注意的是,当前端以json格式向后台传password的值,且后台是以实体User接收时,这时候@JsonIgnore会忽略,即不接收password字段的值。若想避免此类情况,建议使用form表单的形式提交参数,而非json格式。

二、@JsonFormat:

作用:

Date和String的相互转化

时差调整

使用场景:

一般后台传值给前台时

在我们中国来讲和我们的北京时间,会相差8个小时,因为我们是东八区(北京时间)。所以我们在格式化的时候要指定时区(timezone )

Date和String的自动转化

import com.fasterxml.jackson.annotation.JsonFormat;

/**

* 后台返给前台时, 日期自动格式化

*/

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")

private Date birth;

指定时区:

/更新时间 用户可以点击更新,保存最新更新的时间。/

@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8")

private Date updateTime;

三、@JsonInclude

作用:

在将 java pojo 对象序列化成为 json 字符串时,使用 @JsonInclude 注解可以控制在哪些情况下才将被注解的属性转换成 json,例如只有属性不为 null 时。

@JsonInclude(JsonInclude.Include.NON_NULL) //类前面使用,如果为空则不反悔该属性json

public class SellerInfoEntity {

private String id;

private String username;

@JsonInclude(JsonInclude.Include.NON_EMPTY) //属性前使用,如果为空则不返回该属性json

private String password;

private String openid;

private Timestamp createTime;

private Timestamp updateTime;

public SellerInfoEntity() {

}

public SellerInfoEntity(String id, String username, String password, String openid) {

this.id = id;

this.username = username;

this.password = password;

this.openid = openid;

}

}

原文链接:https://blog.csdn.net/qq_44929168/article/details/128374149