Announcement Announcement Module
Collapse
No announcement yet.
nulls and "content" in json using REST and jackson library Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • nulls and "content" in json using REST and jackson library

    Hi

    Using Spring REST and JaxB and Jackson works really well thanks. There are a couple of things I'd like to alter for my application though. I want to change the json output from my Spring REST web service. I'd like to get rid of the nulls in the json output which is automatically created by the framework. However the configuration I've found on by doing a search on google throws no errors but also has no effect. Jackson part of config below. Also the json format also seems to have a "content" that is attributed to the element contents.Can someone tell me how to change this tag to something shorter? Many thanks in advance.
    Code:
    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
                    <property name="objectMapper" ref="jacksonObjectMapper" />
                </bean>
            </list>
        </property>
    </bean>
    
    <!-- jackson configuration : http://stackoverflow.com/questions/3661769 -->
    <bean id="jacksonObjectMapper" class="org.codehaus.jackson.map.ObjectMapper" />
    <bean id="jacksonSerializationConfig" class="org.codehaus.jackson.map.SerializationConfig"
        factory-bean="jacksonObjectMapper" factory-method="getSerializationConfig" />
    <bean
        class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetObject" ref="jacksonSerializationConfig" />
        <property name="targetMethod" value="setSerializationInclusion" />
        <property name="arguments">
            <list>
                <value type="org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion">NON_NULL</value>
            </list>
        </property>
    </bean>

  • #2
    ok - I've fixed the first issue by reading other posts on the net and trial and error.
    commenting out the mvc:annotation driven does then allow me to override the jacksonObjectMapper with the configuration as above. But then I loose the auto xml responses from my jaxb so I then needed to just add an entry for this into the entry into the newly defined annotationMethodHandler <bean class="org.springframework.http.converter.xml.Jaxb 2RootElementHttpMessageConverter"> </bean>

    Then I get content negotiation for application/xml and application/json types in the accept headers with the json responses omitting null properties.

    working config below for my REST mvc app:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    	xmlns:mvc="http://www.springframework.org/schema/mvc"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schem...ring-beans.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schem...ontext-3.0.xsd
    		http://www.springframework.org/schema/jdbc http://www.springframework.org/schem...g-jdbc-3.0.xsd
    		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    
    <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
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
                    <property name="objectMapper" ref="jacksonObjectMapper" />
                </bean>
                <bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"> </bean>
            </list>
        </property>
    </bean>
    
    <!-- jackson configuration : http://stackoverflow.com/questions/3661769 -->
    <bean id="jacksonObjectMapper" class="org.codehaus.jackson.map.ObjectMapper" />
    <bean id="jacksonSerializationConfig" class="org.codehaus.jackson.map.SerializationConfig"
        factory-bean="jacksonObjectMapper" factory-method="getSerializationConfig" />
    <bean
        class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetObject" ref="jacksonSerializationConfig" />
        <property name="targetMethod" value="setSerializationInclusion" />
        <property name="arguments">
            <list>
                <value type="org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion">NON_NULL</value>
            </list>
        </property>
    </bean>
    
    </beans>
    Last edited by jonathanW; Aug 16th, 2011, 07:29 AM.

    Comment

    Working...
    X