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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38828536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 03:48:52  来源:igfitidea点击:

How to ignore "null" or empty properties in json, globally, using Spring configuration

javajsonspringspring-mvcHymanson

提问by Pedro Silva

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? (avoiding XML, preferably)

所以,我的问题是:有没有办法通过 spring 配置全局配置它?(最好避免使用 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 配置它,我在其他问题中找不到。

回答by Jon Peterson

If you are using Spring Boot, this is as easy as:

如果您使用的是 Spring Boot,这很简单:

spring.Hymanson.serialization-inclusion=non_null

If not, then you can configure the ObjectMapper in the MappingHymanson2HttpMessageConverter like so:

如果没有,那么您可以像这样在 MappingHymanson2HttpMessageConverter 中配置 ObjectMapper:

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

回答by Abolfazl Hashemi

If you use Hymanson ObjectMapper for generating json, you can define following custom ObjectMapper for this purpose and use it instead:

如果您使用 Hymanson ObjectMapper 生成 json,则可以为此目的定义以下自定义 ObjectMapper 并使用它:

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

回答by aribeiro

The programmatic alternative to Abolfazl Hashemi's answer is the following:

Abolfazl Hashemi答案的编程替代方案如下:

/**
 * Hymanson configuration class.
 */
@Configuration
public class HymansonConfig {

    @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 ObjectMapperis used, only properties with non-null values are to be included in the mappings.

这样,您基本上是在告诉 Spring 容器,每次使用 anObjectMapper时,映射中只包含具有非空值的属性。