Sources
- https://stackoverflow.com/questions/64868946/how-to-not-return-null-value-in-responseentity
- https://www.baeldung.com/jackson-ignore-null-fields
- https://stackoverflow.com/questions/11757487/how-to-tell-jackson-to-ignore-a-field-during-serialization-if-its-value-is-null
Issue
We have the following setting in the application.yml
file, but it is ignored:
spring:
jackson:
default-property-inclusion: non_null
Manually annotating the classes with
@JsonInclude(JsonInclude.Include.NON_NULL)
did reinstate the wanted behaviour. However, this is painful for classes that are pulled from libraries.
Root cause seems to be related to the following: https://stackoverflow.com/questions/62819472/spring-jackson-default-property-inclusion-ignored.
ObjectMapper
Bean in our config. Add the following: .serializationInclusion(JsonInclude.Include.NON_NULL)
.