Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot jackson non_null property not working

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)
like image 516
Ravat Tailor Avatar asked Jan 27 '23 19:01

Ravat Tailor


2 Answers

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.

like image 155
Konrad Botor Avatar answered Jan 31 '23 09:01

Konrad Botor


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);
    }
}
like image 28
Naresh Muthyala Avatar answered Jan 31 '23 09:01

Naresh Muthyala