According to the documentation the correct answer is:
spring.jackson.default-property-inclusion=non_null
(note the lowercase non_null - this may be the cause of your problem)
Edit:
I've created a simple Spring Boot 1.5.7.RELEASE project with only the following two compile dependencies:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
Then I added the following controller and response classes (using Lombok to skip some boilerplate code):
@RestController
@RequestMapping("/jackson")
public class JacksonTestController {
@GetMapping("/test")
public Response test() {
val response = new Response();
response.setField1("");
return response;
}
}
@Data
class Response {
private String field1;
private String field2;
private Integer field3;
}
Finally I configured Jackson as per documentation, run the application and navigated to http://localhost:8080/jackson/test
. The result was (as expected):
{"field1":""}
After that I dug into Spring Boot's source code and discovered that Spring uses class org.springframework.http.converter.json.Jackson2ObjectMapperBuilder
to create instances of com.fasterxml.jackson.databind.ObjectMapper
. I then put a breakpoint in method public <T extends ObjectMapper> T build()
of aforementioned builder class and run my application in debug mode.
I discovered that there are 8 instances of ObjectMapper
created during application startup and only one of them is configured using contents of application.properties
file. The OP never specified how exactly he was using the serialization, so it's possible his code referred to one of the other 7 object mappers available.
At any rate, the only way to ensure that all object mappers in the application are configured to serialize only non-null properties is to create one's own copy of class org.springframework.http.converter.json.Jackson2ObjectMapperBuilder
and etiher hard code that option as default or customize the class to read application.properties
during every call to it's constructor or build method.