Jackson ObjectMapper setSerializationInclusion()不起作用

时间:2016-05-24 07:17:43

标签: java json jackson

我只是熟悉杰克逊的装订。但是,当我测试setSerializationInclusion(JsonInclude.Include.NON_NULL)时,我发现它有时无法正常工作。

这是我的代码

package com.blithe.main;

import com.blithe.model.Student;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Jackson_2_NullValue {
    public static void main(String[] args) throws JsonProcessingException {

        ObjectMapper mapper = new ObjectMapper();


        Student s = new Student();
        String stundetString = mapper.writeValueAsString(s);
        System.out.println(stundetString);


        // exclude null fields
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

        s.setName("ss");
        stundetString = mapper.writeValueAsString(s);
        System.out.println(stundetString);
    }
}

和POJO

package com.blithe.model;

import java.util.Date;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

// @JsonIgnoreProperties(ignoreUnknown = true)
// exclude null fields for the whole class
// @JsonInclude(Include.NON_NULL)
public class Student {

    // exclude the field whe it's empty ("")
    // @JsonInclude(value=Include.NON_EMPTY)
    private String name;

    private Integer age;

    private Date birth;

    // Jackson ignores it
    @JsonIgnore
    private String nickName;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

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

    public Date getBirth() {
        return birth;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }
}

输出

{"name":null,"age":null,"birth":null}
{"name":"ss","age":null,"birth":null}

后一个应该排除空值,但它没有。

然而,当我以这种方式放置代码时。

package com.blithe.main;

import com.blithe.model.Student;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Jackson_2_NullValue {
    public static void main(String[] args) throws JsonProcessingException {

        ObjectMapper mapper = new ObjectMapper();
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

        Student s = new Student();
        String stundetString = mapper.writeValueAsString(s);
        System.out.println(stundetString);


        // exclude null fields
        // mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

        s.setName("ss");
        stundetString = mapper.writeValueAsString(s);
        System.out.println(stundetString);
    }
}

它适用于

下面的输出
{}
{"name":"ss"}

这是正常还是只是某种错误?我想念什么吗?唯一的maven依赖是jackson-databind 2.7.4。欢迎任何讨论。谢谢!

2 个答案:

答案 0 :(得分:4)

使用时请勿更改ObjectMappers设置。一旦使用映射器,由于序列化器和反序列化器的缓存,并非所有设置都生效。

配置一次实例,第一次使用后不要更改设置。它通过这种方式实现了线程安全性和性能。

http://wiki.fasterxml.com/JacksonFAQThreadSafety http://wiki.fasterxml.com/JacksonBestPracticesPerformance

答案 1 :(得分:0)

所以关键是如果你在多个地方使用ObjectMappers,尽量不要一次又一次地创建对象。它需要首先初始化的配置。

如果你在全球范围内不断变化,它将无法运作。