Ignore empty elements from json body with jackson

I am creating a json body using jackson in java. It will be something like below

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

Based on different REST URIs, the json body should ignore some fields or elements. How to ignore a part of the “student” from the above json body using jackson? When I ignore it, I can only get

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

but I get it as below, which is wrong -

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

I have two classes with getters and setters, one of which is the subject, and the other is the student. I tried using @JsonIgnore, but it ignores all URIs that I don't want. I also tried @JsonInclude (Include.NON_EMPTY). How to achieve this?

. REST URI json . , 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;
        }

}
    }
   }
     }
+4
3

duplicate.

2

@JsonView @JsonFilter

, @JsonView JSON:

{ "": "", "": "100" }

{ "": "", "": "100", "": { "": "", "": "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));
    }
}
+1

, JsonInclude NON_NULL, NON_EMPTY.

Javadoc NON_EMPTY NON_NULL , , , NON_NULL , Jackson 2.6.2.

@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"}

. ,

mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
+3

WRITE_NULL_MAP_VALUES false ObjectMapper.

:

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

Source: https://habr.com/ru/post/1671034/


All Articles