Search code examples
jsonjacksonspring-bootjersey-2.0

Spring Boot 1.2.5 & Jersey 2 ignore null fields


In Spring boot 1.2.5 with a Jersey 2 interface, how can I set the JSON marshaller to not include fields that have null values?

For example:

[
    {
        "created": 1433987509174,
        "lastModified": 1433876475580,
        "id": 1,
        "example": "example1b"
    },
    {
        "created": 1434502031212,
        "lastModified": 1434502031212,
        "id": 10000,
        "example": "example1c"
    },
    {
        "created": 1439151444176,
        "lastModified": 1439151444176,
        "id": 10011,
        "example": null
    }
]

The field "example": null should not be included in the json output at all, but here it is specifying it is null.

In my @SpringBootApplication class, I've tried adding:

@Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
    final MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
    final ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    converter.setObjectMapper(objectMapper);
    return converter;
}

or

@Bean
@Primary
public Jackson2ObjectMapperBuilder objectMapperBuilder() {
    Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
    builder.serializationInclusion(JsonInclude.Include.NON_NULL);
    return builder;
}

or

@Primary
@Bean
public ObjectMapper mapper() {
    final ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    return objectMapper;
}

and/or adding @JsonSerialize(include = Inclusion.NON_NULL) to the Object itself

But it still produces the same response above with the "example": null included.

It was working on Spring 3.0.7 with @JsonSerialize(include=Inclusion.NON_NULL) but that no longer works now that I've ported to Spring Boot 1.2.5.

I believe I've followed the documentation http://docs.spring.io/spring-boot/docs/current/reference/html/howto-spring-mvc.html#howto-customize-the-jackson-objectmapper and it's not working so I'm hoping someone might see something I'm missing? Thanks in advance!

Edit: Also just tried adding the class:

@Configuration
public class WebConfiguration extends WebMvcAutoConfiguration {
    @Primary
    @Bean
    public ObjectMapper mapper() {
        final ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        return objectMapper;
    }
}

Solution:

package com.my.spring;

import javax.ws.rs.ext.ContextResolver;

import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.servlet.ServletProperties;
import org.springframework.context.annotation.Configuration;

import com.my.spring.service.rs.MyRestServiceImpl;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;

@Configuration
public class JerseyConfiguration extends ResourceConfig {

    public class ObjectMapperContextResolver implements ContextResolver<ObjectMapper> {
        private final ObjectMapper mapper;

        public ObjectMapperContextResolver() {
            mapper = new ObjectMapper();
            mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        }

        @Override
        public ObjectMapper getContext(Class<?> type) {
           return mapper;
        }
    }

    public JerseyConfiguration() {
        register(new ObjectMapperContextResolver());
        register(MyRestServiceImpl.class); // My jax-rs implementation class
        property(ServletProperties.FILTER_FORWARD_ON_404, true); // Not needed for this non_null issue
    }
}

Solution

  • I don't know about mixing the Spring way (of configuring the mapper) and how Jersey handles this. But the Jersey way to configure the ObjectMapper is through a ContextResolver, as seen in this answer.

    Then register the ObjectMapperContextResolver with your Jersey configuration.

     public JerseyConfig extends ResourceConfig {
         public JerseyConfig() {
             ...
             register(ObjectMapperContextResolver.class);
         }
     }
    

    Or if you are package scanning, the @Provider annotation will pick up the class.