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

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

@JsonIgnore

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

public class User {
    @JsonIgnore
    private String username;
    private String password;
    private Integer age;
    }
    @RequestMapping("/a")
    public User test(@RequestBody User u) {
        User user = new User();
        user.setUsername("wkw");
        return user;
    }

访问这个接口返回的参数就不包含username这个字段了

{
  "password": null,
  "age": null
}

然而我们访问这个接口带过去的参数username这个字段也不会反序列化

{
   "username" : "小明",
   "password" : "123",
   "age" : 15
}

反序列化username属性为空
在这里插入图片描述
由此可见当我们需要在序列化和反序列化的时候忽略某个字段的时候就用这个注解加在字段上面就行了。

@JsonIgnoreProperties

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

@JsonIgnoreProperties({"username","password"})
public class User {
    private String username;
    private String password;
    private Integer age;
    }

这样子useranme和password这两个字段在序列化和反序列化的时候就都忽略了。

{
  "age": null
}

还有一个用法就是配合allowGetters,allowSetters一起用用来控制字段忽视是在序列化还是反序列化,这样更加灵活,如下所示

//get为true的时候说明字段允许序列化,反序列的时候忽略该字段
@JsonIgnoreProperties(value = {"usname","password"},allowGetters = true)
public class User {
    private String username;
    private String password;
    private Integer age;
    }
//set为true说明字段允许反序列化,序列化的时候忽略该字段
@JsonIgnoreProperties(value = {"usname","password"},allowSetters = true)
public class User {
    private String username;
    private String password;
    private Integer age;

一般情况下,如果json字段转换为实体类即反序列化的时候,有字段无法对应实体类即会报错json解析异常,比如下面的json中我们接受的实体类User当中没有height这个字段,接收的时候就会有异常

{
   "username" : "小明",
   "password" : "123",
   "age" : 15
   "height" : 168
}
Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unexpected character ('"' (code 34)): was expecting comma to separate Object entries; nested exception is com.fasterxml.jackson.core.JsonParseException: Unexpected character ('"' (code 34)): was expecting comma to separate Object entries
 at [Source: (PushbackInputStream); line: 5, column: 5]]

这时只需要在实体类上面加注解即可解决,ignoreUnknown设置为true说明在反序列化的时候忽视未知的字段,即反序列的时候能对应的就对应上,不能对应的就不管了。

@JsonIgnoreProperties(ignoreUnknown = true)
public class User {
    private String username;
    private String password;
    private Integer age;
    }

@JsonIgnoreType

这个注解是用在类上面的表明这个类在序列化和反序列化的时候被忽略
比如我们下面有个User类,类上面加了注解 @JsonIgnoreType

@JsonIgnoreType
public class User {
    private String username;
    private String password;
    private Integer age;
    }

然后我们有个Param类,里面的字段包含User类

public class Param {
    private String type;
    private User user;
}

我们写个接口返回这个Param看看json是啥?

    @RequestMapping("/a")
    public Param test() {
        User user = new User();
        user.setUsername("wkw");
        user.setPassword("123");
        Param param = new Param();
        param.setType("type");
        param.setUser(user);
        return param;
    }

结果如下,说明我们的这个User类在序列化的时候被忽略了,对于反序列化也是一样的。

{
  "type": "type"
}

结尾

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

猜你喜欢

转载自blog.csdn.net/weixin_44130081/article/details/89671016