使用jackson忽略json body中的空元素

时间:2017-02-28 12:26:02

标签: java json jackson

我正在java中使用jackson构建一个json体。它将类似于下面的

{
   "subject": "math",
    "marks": "100",
    "student":{
        "name": "x",
        "class": "8"
    }
 }

基于不同的REST URI,json主体必须忽略一些字段或元素。我如何忽略"学生"部分来自上面json身体使用杰克逊? 当我忽略它时,我应该只能得到

{ "subject": "math", "marks": "100"}

但是如下所示:

{ "subject": "math", "marks": "100","student":{}}

我有两个有getter和setter的课程,一个是主题,另一个是学生。我尝试使用@JsonIgnore,但它忽略了我不想要的所有URI。我也试过@JsonInclude(Include.NON_EMPTY)。我如何实现这一目标?

我在这里添加我的代码。基于不同的REST URI,json主体必须忽略一些字段或元素。例如,对于一个URI,它应该包括所有字段,对于另一个URI,它应该忽略学生。        得分=新得分();

    score.setSubject("math");
    score.setMarks("100");

    Score.Student student =score.new Student();
    score.setStudent(student);

    switch (type) {
    case StudentAdd:
        score.setSubject("math");
        score.setMarks("100");
        break;
    case StudentDelete:
        score.setSubject("math");
        score.setMarks("100");
        break;
    case StudentComplete:
        score.setChangeReason("C");

    default:
        break;
        score.setSubject("math");
        score.setMarks("100");

    score.setStudent(student);

    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
    //objectMapper.setSerializationInclusion(Include.NON_NULL);
    objectMapper.setSerializationInclusion(Include.NON_EMPTY);

    StringWriter jsonBody = new StringWriter();
    objectMapper.writeValue(jsonBody, score);

    return jsonBody.toString();
}

    //@JsonInclude(Include.NON_EMPTY)
    class Score {

        private String subject;
        private String marks;
        private Student student;

        public String getSubject() {
            return subject;
        }

        public void setSubject(String subject) {
            this.subject = subject;
        }

        public String getMarks() {
            return marks;
        }

        public void setMarks(String marks) {
            this.marks = marks;
        }

        public Student getStudent() {
            return student;
        }

        public void setStudent(Student student) {
            this.student = student;
        }

    //@JsonInclude(Include.NON_NULL)
    class Student {

        private String name;

        @JsonProperty("class")
        private String clazz;

        public String getName() {
            return name;
        }

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

        public String getClazz() {
            return clazz;
        }

        public void setClazz(String clazz) {
            this.clazz = clazz;
        }

}
    }
   }
     }

3 个答案:

答案 0 :(得分:3)

可以使用值JsonInclude而不是NON_NULL的{​​{1}}对注释进行配置。

NON_EMPTY与NON_NULL的Javadoc不太清楚,但我的实验表明使用NON_NULL可以正常使用Jackson 2.6.2。

NON_EMPTY

实施例

@JsonProperty
@JsonInclude(Include.NON_NULL)
private Student student;

输出

ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.writeValueAsString(new Foo(100, new Student("bob"), "math")));
System.out.println(mapper.writeValueAsString(new Foo(100, null, "math")));

请注意,这也可以使用

进行全局设置
{"marks":100,"subject":"math","student":{"name":"bob"}}
{"marks":100,"subject":"math"}

答案 1 :(得分:1)

修改

此帖子似乎是duplicate

编辑2

使用@JsonView代替@JsonFilter

下面是使用@JsonView打印同一JSON对象的不同视图的示例代码:

  

{&#34受试者#34;:"数学""马克":" 100"}

  

{&#34受试者#34;:"数学""马克":" 100""学生":{ "名称":" X""类":" 8"}}

public class Views {
    public static class Filtered {}
    public static class All extends Filtered {}
}

public class Student {
    private String name;

    @JsonProperty("class")
    private String clazz;

    public String getName() {
        return name;
    }

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

    public String getClazz() {
        return clazz;
    }

    public void setClazz(String clazz) {
        this.clazz = clazz;
    }
}

public class Score {
    @JsonView(Views.Filtered.class)
    private String subject;

    @JsonView(Views.Filtered.class)
    private String marks;

    @JsonView(Views.All.class)
    private Student student;

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getMarks() {
        return marks;
    }

    public void setMarks(String marks) {
        this.marks = marks;
    }

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }
}

public class JsonTest {
    public static void main(String[] args) throws JsonProcessingException {
        Student student = new Student();
        student.setName("x");
        student.setClazz("8");

        Score score = new Score();
        score.setSubject("math");
        score.setMarks("100");
        score.setStudent(student);

        ObjectMapper mapper = new ObjectMapper();

        //do not serialize student property
        System.out.println(mapper.writerWithView(Views.Filtered.class).writeValueAsString(score));

        //also serialize student property
        System.out.println(mapper.writeValueAsString(score));
    }
}

答案 2 :(得分:0)

在ObjectMapper中将WRITE_NULL_MAP_VALUES设置为false。

代码:

    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(SerializationConfig.Feature.WRITE_NULL_MAP_VALUES, false);