开发者

Define an custom jackson objectmapper for a spring3 mvc project

I am using mvc:annotation-driven which meant that simply puttin the jackson jar files onto the classpath configured the Jackson json marhsaller. Now I 开发者_运维问答would like to provide a custom object mapper to be able to control the serialization of Date fields on a global level.

I began to define the AnnotationMethodhandlerAdapter and referenced my own jsonConverter bean (see below)

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="jsonConverter" />
            <ref bean="xmlMarshallingConverter" />
        </list>
    </property>
</bean>

The problem is dates are still written as timestamps. The custom objectmapper bean is created (got logs for that), but it seesm the objectmapper is never used. I assume it still uses the default MappingJacksonHttpMessageConverter and the default ObjectMapper.

Any ideas? How would I figure out what exact beans to overwrite to chagne the default mapper?

Thanx!


Have you configured the view resolver to also use your custom object mapper instance (see reference to jacksonObjectMapper below)?

It's my understanding that AnnotationMethodHandlerAdapter handles the inbound conversions and ContentNegotiatingViewResolver handles outbound conversions.

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="mediaTypes">
        <map>
            <entry key="json" value="application/json"/>
            <!-- xml etc -->
        </map>
    </property>
    <property name="defaultViews">
        <list>
            <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
                <property name="objectMapper" ref="jacksonObjectMapper"/>
            </bean>
            <!-- xml etc -->
        </list>
    </property>
</bean>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜