user3537498
user3537498

Reputation: 21

Spring Boot Microservice Jackson JSON serialisation NON NULL

I'm currently working on a Spring Boot ( Version 1.3.1 ) Microservice which connects to MongoDB backend and provides the backend data ( Ex: Provider object ) to the client via controller.

The project has got one class file which extends ResourceSupport ( Ex: ProviderResourceSupport ) and also another class which extends ResourceSupportAssembler class ( Ex: ProviderAssembler ) for generating Links to the Response objects.

Ideally my requirement is to customise the JSON objects on a need basis and as such using @JsonView ( followed this link - https://spring.io/blog/2014/12/02/latest-jackson-integration-improvements-in-spring ) and added Spring Jackson dependencies in the maven project.

I have also added spring.jackson.serialization-inclusion=non-null & spring.jackson.serialization.indent_output=true in the application.properties.

For one of the method in the controller, the response will be 'ResponseEntity< List< ProviderResourceSupport>>' , and this method is returning with a 'null' response if the data is not present.

I have added @JsonInclude(Include=NON_NULL) on my entity objects and controllers but still getting the 'null' response.

I don't want the 'null' as the response and request you to help me incase if anyone has faced the similar issue.

Upvotes: 0

Views: 1056

Answers (1)

user2669657
user2669657

Reputation: 575

I fixed this null properties escaping from json response extending a Jackson Mapper Bean but I don't use Spring Boot, take a quickly look and check if this is suitable for you

public class Jackson2ObjectMapperCustom extends Jackson2ObjectMapperFactoryBean {

    @Override
    public void afterPropertiesSet() {
        super.afterPropertiesSet();

        getObject().setSerializationInclusion(JsonInclude.Include.NON_NULL).setSerializationInclusion(JsonInclude.Include.NON_EMPTY);

        Hibernate5Module hibernateModule = new Hibernate5Module();
        hibernateModule.disable(Feature.USE_TRANSIENT_ANNOTATION);
        hibernateModule.enable(Feature.FORCE_LAZY_LOADING);
        getObject().registerModules(new JavaTimeModule(), hibernateModule);
        getObject().configure(SerializationFeature.INDENT_OUTPUT, true);
        getObject().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        getObject().setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"));
    }
}

And in my case I use Spring Xml configuration

<bean id="objectMapper" class="com.xxx.common.Jackson2ObjectMapperCustom" />

<mvc:annotation-driven>
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
        <bean class="org.springframework.http.converter.ResourceHttpMessageConverter"/>
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <property name="objectMapper" ref="objectMapper"/>
        </bean>
    </mvc:message-converters>       
</mvc:annotation-driven>

Upvotes: 0

Related Questions