Announcement Announcement Module
Collapse
No announcement yet.
Setting SerializationInclusion to NON_NULL with Jackson Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Setting SerializationInclusion to NON_NULL with Jackson

    Hi all,

    I have searched the web and the forums for an answer, but I have not found one to this specific problem that has worked for me. I want to leave out the null fields in the JSON response. But, I'm using Spring 3.1.1 and would like to use annotation-driven.

    Here is the Jackson dependency:

    Code:
    <dependency>
          <groupId>org.codehaus.jackson</groupId>
          <artifactId>jackson-mapper-asl</artifactId>
          <version>1.9.1</version>
    </dependency>

    Here is the code block for adding the custom jackson message converter:
    Code:
    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="com.patrol.jackson.CustomJacksonMessageConverter"/>
        </mvc:message-converters>
    </mvc:annotation-driven>
    Here is the CustomJacksonMessageConverter class:
    Code:
    public class CustomJacksonMessageConverter extends MappingJacksonHttpMessageConverter {
        public CustomJacksonMessageConverter() {
            super();
            getObjectMapper().getSerializationConfig().setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
        }
    }
    I've also tried this using XML to set the inclusion to NON_NULL, and using older versions of jackson.

    No matter what I try, it looks like my code is generated with the correct object mapper. Then a default converter is generated, where the object mapper is null. Finally, when I hit my webservice url, null fields are still in the response.

    Is it possible to set the Inclusion to NON_NULL with Spring 3.1.1 while using annotation-driven? If so, please show me what I'm doing wrong.

    Thank you,
    Mike
Working...
X