Configuration for REST spring mvc with Jackson json mapper omitting null properties

Putting this configuration into my spring res-mvc-config.xml changed the json responses so that null properties are not written. To make this changes have an effect you have to get rid of any mvc:annotation-driven element as this stops the overriding having an effect. no errors will be thrown, so it’s not easy to see what is going on or not going on 😉

<<?xml version=”1.0″ encoding=”UTF-8″?>
<beans xmlns=”http://www.springframework.org/schema/beans&#8221;
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;
xmlns:context=”http://www.springframework.org/schema/context&#8221;
xmlns:jdbc=”http://www.springframework.org/schema/jdbc&#8221;
xmlns:mvc=”http://www.springframework.org/schema/mvc&#8221;
xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd”&gt;

<context:component-scan base-package=”uk.ac.sanger.das.dasregistry.springrest.sources”></context:component-scan>

<context:component-scan base-package=”uk.ac.sanger.das.dasregistry.springrest.coordinates”></context:component-scan>
<!– <mvc:annotation-driven/> –>

<bean
>
<property name=”messageConverters”>
<list>
<bean> </bean>
<bean>
<property name=”objectMapper” ref=”jacksonObjectMapper” />
</bean>
</list>
</property>
</bean>

<!– jackson configuration : http://stackoverflow.com/questions/3661769 –>
<bean id=”jacksonObjectMapper” />
<bean id=”jacksonSerializationConfig”
factory-bean=”jacksonObjectMapper” factory-method=”getSerializationConfig” />
<bean
>
<property name=”targetMethod” value=”setSerializationInclusion” />
<property name=”targetObject” ref=”jacksonSerializationConfig” />
<property name=”arguments”>
<list>
<value type=”org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion”>NON_NULL</value>
</list>
</property>
</bean>

</beans>

Advertisements
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: