0

I am using Spring Boot 1.5.4 version . I am using spring-ws getWebServiceTemplate() to make a webservice call. The SOAP response has lot of null values for the fields.

I am trying to filter out the null values in the JSON response. None of the following approaches seem to work:

  1. Setting the property in the application.properties:
spring.jackson.default-property-inclusion:NON_NULL`
  1. Setting it in Configuration class using Jackson2ObjectMapperBuilder:
@Bean
    public Jackson2ObjectMapperBuilder objectMapperBuilder() {
        Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();        
        builder.serializationInclusion(JsonInclude.Include.NON_NULL);
        builder.serializationInclusion(JsonInclude.Include.NON_EMPTY);
        return builder;
    } 

Please advise.

lva

2 Answers 2

2

I am using Spring Boot 1.5.6.RELEASE version, and you can reference customize-the-jackson-objectmapper

Following code is work:

 @SpringBootApplication
    public class Application {

        @Bean
        public Jackson2ObjectMapperBuilder objectMapperBuilder() {
            Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
            builder.serializationInclusion(JsonInclude.Include.NON_NULL);
            return builder;
        }

        public static void main(String[] args) {
            SpringApplication.run(DbeeApiApplication.class, args);
        }
    }

Or you can filter from MappingJackson2HttpMessageConverter, for example:

@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);
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Please provide some explanation to your answer as to how it will solve the issue. That will also help avoid same issues in future. Code-only answers are not highly appreciated.
1

Using the following in application.properties worked.

spring.jackson.default-property-inclusion=NON_NULL

1 Comment

nice and clean solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.