本文介绍了Spring Boot 1.2.5&泽西2忽略空字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

例如:

[
    {
        "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输出中,但是这里指定它为null 。

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

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

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

@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)添加到对象本身

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

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

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

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

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!

修改:还尝试添加课程:

@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如何处理它。但是,配置 ObjectMapper 的Jersey方法是通过 ContextResolver ,如。

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.

然后注册 ObjectMapperContextResolver 使用。

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

或者如果你是包扫描, @Provider 注释将选择该课程。

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

这篇关于Spring Boot 1.2.5&amp;泽西2忽略空字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 06:48