Spring Boot 1.2.5 和 Jersey 2 忽略空字段

Spring Boot 1.2.5 & Jersey 2 ignore null fields

在使用 Jersey 2 接口的 Spring boot 1.2.5 中,如何设置 JSON 编组器不包含具有空值的字段?

例如:

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

字段 "example": null 根本不应包含在 json 输出中,但此处指定它为空。

在我的@SpringBootApplication class 中,我尝试添加:

@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;
}

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

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

and/or 将 @JsonSerialize(include = Inclusion.NON_NULL) 添加到对象本身

但它仍然会产生与上面相同的响应,其中包含 "example": null

它在 Spring 3.0.7 和 @JsonSerialize(include=Inclusion.NON_NULL) 上工作,但现在我已经移植到 Spring Boot 1.2.5。

我相信我已经遵循了文档 http://docs.spring.io/spring-boot/docs/current/reference/html/howto-spring-mvc.html#howto-customize-the-jackson-objectmapper 但它不起作用,所以我希望有人能看到我遗漏的东西?提前致谢!

编辑: 还尝试添加 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;
    }
}

解法:

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
    }
}

我不知道混合 Spring 方式(配置映射器)以及 Jersey 如何处理这个问题。但是 Jersey 配置 ObjectMapper 的方法是通过 ContextResolver,如 .

所示

然后用your Jersey configuration注册ObjectMapperContextResolver

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

或者如果您正在扫描包,@Provider 注释将选择 class。