I know about these annotations @JsonInclude(JsonInclude.Include.NON_NULL) and @JsonInclude(JsonInclude.Include.EMPTY) but in my case it's doesn't work.
My case is:
I have some class (entity (SomeClass)) with some other entity inside (SomeObject)
@Data
public class SomeClass {
private String fieldOne;
@JsonInclude(JsonInclude.Include.NON_NULL)
private String fieldTwo;
@JsonInclude(JsonInclude.Include.NON_NULL)
private SomeObject someObject;
}
Entity - SomeObject
@Data
public class SomeObject {
@JsonInclude(JsonInclude.Include.NON_NULL)
private String name;
}
Main class
public class Main {
public static void main(String[] args) throws JsonProcessingException {
SomeClass someClass = new SomeClass();
someClass.setFieldOne("some data");
SomeObject someObject = new SomeObject();
someObject.setName(null);
someClass.setSomeObject(someObject);
ObjectMapper objectMapper = new ObjectMapper();
String someClassDeserialized = objectMapper.writeValueAsString(someClass);
System.out.println(someClassDeserialized);
}
}
Output
{"fieldOne":"some data","someObject":{}}
The final output should be, without object (SomeObject) with null or empty fields:
{"fieldOne":"some data"}