【问题标题】:Spring Boot 1.2.5 & Jersey 2 ignore null fieldsSpring Boot 1.2.5 & Jersey 2 忽略空字段
【发布时间】:2015-11-04 08:53:37
【问题描述】:

在带有 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 类中,我尝试添加:

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

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

但它仍然会产生与上述相同的响应,包括 "example": null

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

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

编辑:也只是尝试添加类:

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

【问题讨论】:

    标签: json jackson spring-boot jersey-2.0


    【解决方案1】:

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

    然后用your Jersey configuration注册ObjectMapperContextResolver

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

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

    【讨论】:

    • 谢谢!这是有道理的,因为我选择了 JAX-RS 接口,所以使用了 jersey 映射器而不是 spring 映射器。此解决方案有效。不知何故,SO 问题并没有出现在我的搜索中,或者它可以为我节省不少困惑。
    猜你喜欢
    • 2021-08-18
    • 2015-11-25
    • 1970-01-01
    • 2021-04-19
    • 2015-10-05
    • 2015-12-06
    • 1970-01-01
    • 2021-04-23
    • 2016-09-04
    相关资源
    最近更新 更多