Reputation: 1263
I am using Spring boot 1.5.7-RELEASE.
I am trying to set global non null Jackson property in my application.
but it is not working.
I have tried both in application.properties and bootstrap.properties but not working.
spring.jackson.default-property-inclusion=NON_NULL
spring.jackson.serialization-inclusion=NON_NULL
but when I applied on class level, it is working fine.
@JsonInclude(JsonInclude.NON_NULL)
Upvotes: 12
Views: 24086
Reputation: 5063
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 the documentation, ran 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 the aforementioned builder class and ran 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 either hard code that option as default or customize the class to read application.properties
during every call to its constructor or build method.
Upvotes: 13
Reputation: 611
I was just dealing with the settings in application.properties
not taking either. In my case, there was an abstract config class I was extending which defined an ObjectMapper
bean that had totally different settings. So I had to override it.
What brought me to the place of finding that was using the /beans
Actuator endpoint that Spring Boot apps have, and searching for 'ObjectMapper
'. It revealed an instance I didn't realize was being created.
Upvotes: 0
Reputation: 163
Maybe I'm late to the party but It may help someone.
Extend WebMvcConfigurationSupport class and customize the Springboot configuration the way you want.
@Configuration
public class Config extends WebMvcConfigurationSupport{
@Override
protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
converter.setObjectMapper(mapper);
converters.add(converter);
super.configureMessageConverters(converters);
}
}
Upvotes: 6