Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

So, my question is: Is there a way to configure this globally through spring config? (avoiding XML, preferably)

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.

like image 341
Pedro Silva Avatar asked Aug 08 '16 11:08

Pedro Silva


People also ask

How do I ignore null values in JSON spring boot?

You can ignore null fields at the class level by using @JsonInclude(Include. NON_NULL) to only include non-null fields, thus excluding any attribute whose value is null. You can also use the same annotation at the field level to instruct Jackson to ignore that field while converting Java object to json if it's null.

How do I ignore null values in JSON request?

In order to ignore null fields at the class level, we use the @JsonInclude annotation with include. NON_NULL.

How do I ignore JSON property?

To ignore individual properties, use the [JsonIgnore] attribute. You can specify conditional exclusion by setting the [JsonIgnore] attribute's Condition property. The JsonIgnoreCondition enum provides the following options: Always - The property is always ignored.

How do I ignore null values in post request body in spring boot?

Just use this @JsonSerialize(include = Inclusion. NON_NULL) instead of @JsonInclude(Include. NON_NULL) and it works..!!


4 Answers

In newer versions of Spring Boot (2.0+), use:

spring.jackson.default-property-inclusion=non_null
like image 89
JRA_TLL Avatar answered Oct 25 '22 06:10

JRA_TLL


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

spring.jackson.serialization-inclusion=non_null

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

@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);
            }
        }
    }
}
like image 44
Jon Peterson Avatar answered Oct 25 '22 05:10

Jon Peterson


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

/**
 * 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.

Another alternative, as per the Spring Boot documentation, for Jackson 2+, is to configure it in the application.properties:

spring.jackson.default-property-inclusion=non_null
like image 26
António Ribeiro Avatar answered Oct 25 '22 06:10

António Ribeiro


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

<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>
like image 36
Abolfazl Hashemi Avatar answered Oct 25 '22 07:10

Abolfazl Hashemi