简体   繁体   English

如何使用 Spring 配置全局忽略 json 中的“null”或空属性

[英]How to ignore “null” or empty properties in json, globally, using Spring configuration

I'm trying to return only the properties that have values, but the null ones are also being returned.我试图只返回具有值的属性,但也返回空值。

I know that there's an annotation that does this ( @JsonInclude(Include.NON_NULL) ), but then I need these in every single entity class.我知道有一个注释可以做到这一点( @JsonInclude(Include.NON_NULL) ),但是我在每个实体类中都需要这些。

So, my question is: Is there a way to configure this globally through spring config?所以,我的问题是:有没有办法通过 spring 配置全局配置它? (avoiding XML, preferably) (最好避免使用 XML)

EDIT: It seems that this question has been considered a duplicate, but I don't think so.编辑:似乎这个问题被认为是重复的,但我不这么认为。 The real question here is how to configure it through spring config, which I couldn't find in other questions.这里真正的问题是如何通过 spring config 配置它,我在其他问题中找不到。

If you are using Spring Boot, this is as easy as:如果您使用的是 Spring Boot,这很简单:

spring.jackson.serialization-inclusion=non_null

If not, then you can configure the ObjectMapper in the MappingJackson2HttpMessageConverter like so:如果没有,那么您可以像这样在 MappingJackson2HttpMessageConverter 中配置 ObjectMapper:

@Configuration
class WebMvcConfiguration extends WebMvcConfigurationSupport {
    @Override
    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        for(HttpMessageConverter converter: converters) {
            if(converter instanceof MappingJackson2HttpMessageConverter) {
                ObjectMapper mapper = ((MappingJackson2HttpMessageConverter)converter).getObjectMapper()
                mapper.setSerializationInclusion(Include.NON_NULL);
            }
        }
    }
}

If you use jackson ObjectMapper for generating json, you can define following custom ObjectMapper for this purpose and use it instead:如果您使用 jackson ObjectMapper 生成 json,您可以为此目的定义以下自定义 ObjectMapper 并使用它:

<bean id="jacksonObjectMapper" class="com.fasterxml.jackson.databind.ObjectMapper">
   <property name="serializationInclusion">
      <value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value>
   </property>
</bean>

The programmatic alternative to Abolfazl Hashemi 's answer is the following: Abolfazl Hashemi答案的编程替代方案如下:

/**
 * Jackson configuration class.
 */
@Configuration
public class JacksonConfig {

    @Bean
    public ObjectMapper buildObjectMapper() {
       return new ObjectMapper().setSerializationInclusion(Include.NON_NULL);
    }
}

This way, you're basically telling to the Spring container that, every time an ObjectMapper is used, only properties with non-null values are to be included in the mappings.这样,您基本上是在告诉 Spring 容器,每次使用ObjectMapper时,映射中只包含具有非空值的属性。

Another alternative, as per the Spring Boot documentation , for Jackson 2+, is to configure it in the application.properties :根据Spring Boot 文档,对于 Jackson 2+ 的另一种选择是在application.properties配置它:

spring.jackson.default-property-inclusion=non_null

在较新版本的 Spring Boot (2.0+) 中,使用:

spring.jackson.default-property-inclusion=non_null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM