I'm trying to remove both null attributes and empty objects from a complex object during serialization using Camel-Jackson and Lombok.
I checked this answer and it works, but they're accessing to the attributes directly without using getters or setters, so when I use Lombok's @Getter and @Setter it doesn't seem to work:
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@Getter
@Setter
public class School {
private String name;
@JsonInclude(value = JsonInclude.Include.CUSTOM, valueFilter = Room.class)
private Room room;
}
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@Getter
@Setter
@EqualsAndHashCode
class Room {
private String name;
}
So when I use this:
School school = new School();
school.setRoom(new Room());
String jsonPerson = mapper.writeValueAsString(school);
System.out.println("Linearized json: "+jsonPerson);
It works
Linearized json: {}
However if I don't create a new Room object for the School instance
School school = new School();
String jsonPerson = mapper.writeValueAsString(school);
System.out.println("Linearized json: "+jsonPerson);
I'm getting
{"room":null}
So what could be the problem here? I also tried with manual Getters and Setters instead of Lombok's and it doesn't work as I expect.
I'm using camel-jackson 3.3 and lombok 1.18.12.