jackson 对boolean 类型属性名为 isXXX 处理

一笑奈何 提交于 2020-01-21 17:45:37

在Java 中, Boolean isGradudate 这种声明的属性,再试用idea 默认生成的get 和 set 方法会默认直接去掉is如下

    public Boolean getGraduated() {
        return isGraduated;
    }

    public void setGraduated(Boolean graduated) {
        isGraduated = graduated;
    }

在序列化和反序列化时,会有所不同。如下demo:(JsonMapper 是jackson实现)

class  Demo {

    private String realName;

    private Integer age;

    private Boolean isGraduated;

    private Boolean work;

    private String account;

    private String password;

    public Boolean getGraduated() {
        return isGraduated;
    }

    public void setGraduated(Boolean graduated) {
        isGraduated = graduated;
    }

    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Boolean getWork() {
        return work;
    }

    public void setWork(Boolean work) {
        this.work = work;
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "Demo{" +
                "realName='" + realName + '\'' +
                ", age=" + age +
                ", isGraduated=" + isGraduated +
                ", work=" + work +
                ", account='" + account + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

测试方法:

public void testDemo() {

        String json = "{\"realName\":\"Victor\",\"age\":22,\"isGraduated\":true,\"work\":true,\"account\":\"aaaaa\",\"password\":\"bbbbb\"}";
        Demo fromData = jsonMapper.fromJson(json, Demo.class);
        System.out.println(fromData.toString());

        Demo demo = new Demo();
        demo.setRealName("Joe");
        demo.setAge(20);
        demo.setAccount("aaaaa");
        demo.setPassword("bbbbb");
        demo.setGraduated(false);
        demo.setWork(true);
        System.out.println(jsonMapper.toJson(demo));
    }

结果:

Demo{realName='Victor', age=22, isGraduated=null, work=true, account='aaaaa', password='bbbbb'}
{"realName":"Joe","age":20,"work":true,"account":"aaaaa","password":"bbbbb","graduated":false}

Jackson 在反序列化成domain 的时候找不到isGraduate 对应的属性(set方法setGraduate,根据set后面的属性名指定json key),所以置为null;

Jackson 在序列化的时候, get 方法是 getGraduate,所以结果显示的是 "graduate":false

在demo 里面, work 同样是Boolean 类型的,但是命名方式不同, 没有采用isWork 方式, 所以就会按照普通的属性进行序列化和反序列化

如果指定想要使用 isGraduate作为参数, 需要自行写get 和 set 方法,这样在序列化和反序列化的时候才会获取到对应的属性名

    public Boolean getIsGraduated() {
        return isGraduated;
    }


    public void setIsGraduated(Boolean graduated) {
        this.isGraduated = graduated;
    }

结果:

Demo{realName='Victor', age=22, isGraduated=true, work=true, account='aaaaa', password='bbbbb'}
{"realName":"Joe","age":20,"isGraduated":false,"work":true,"account":"aaaaa","password":"bbbbb"}

当然,这种情况也可以使用jackson 提供的注解解决:

使用 @JsonProperty 和 @JsonIgnore 处理这种情况

    @JsonProperty("isGraduated")
    private Boolean isGraduated;
    
    @JsonIgnore
    public Boolean getGraduated() {
        return isGraduated;
    }

    public void setGraduated(Boolean graduated) {
        isGraduated = graduated;
    }

@JsonProperty("isGraduated") 指定属性名为 isGraduated, Jackson序列化后显示成 isGraduated,用在属性上

@JsonIgnore  忽略指定的字段,不显示出来, 可用在属性以及get和set 方法上,用在属性上,整个属性不会被显示

@JsonProperty 用在属性上,@JsonIgnore 用在get 方法上可以达到目标效果:

Demo{realName='Victor', age=22, isGraduated=true, work=true, account='aaaaa', password='bbbbb'}
{"realName":"Joe","age":20,"work":true,"account":"aaaaa","password":"bbbbb","isGraduated":false}

单独使用@Jsonproperty在属性上,在序列化的时候,  graduated 和 isGraduated 同时显示,把 @JsonIgnore 放在get 方法上可以忽略掉graduated 只显示 isGraduated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!