Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring and MappingJackson2HttpMessageConverter and registerModule

We are using the latest Spring 4.2.x and we recently went from Jackson Mapper 2.6.3 to 2.8.8, and now we are registering Modules.

Here is part of what or spring-servlet.xml looks like:

<mvc:annotation-driven>
    <mvc:message-converters register-defaults="true">
        <bean
            class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <property name="objectMapper">
                <bean class="com.fasterxml.jackson.databind.ObjectMapper">
                    <property name="dateFormat">
                        <bean class="java.text.SimpleDateFormat">
                            <constructor-arg type="java.lang.String" value="yyyy-MM-dd"></constructor-arg>
                        </bean>
                    </property>
                </bean>
            </property>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

and here is what we us in code to map a json file:

    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new ParameterNamesModule());
    mapper.registerModule(new Jdk8Module());
    mapper.registerModule(new JavaTimeModule());

So, what I'd like to do is configure the "spring-servlet" and "MappingJackson2HttpMessageConverter" so that I can add the modules to register.

Yes, we haven't gotten into using @Configuration yet, we are still using the XML which I don't mind at all.

Thanks for any help!

like image 873
tjholmes66 Avatar asked Jun 06 '26 15:06

tjholmes66


1 Answers

So, after some testing and trial and error, we came up with a springmvc-servlet.xml that looks like:

<context:annotation-config />
<context:component-scan base-package="com.tomholmes.mycode.core.server.controller" />

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

<bean id="objectMapper"
    class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">

    <property name="featuresToDisable">
        <array>
            <util:constant
                static-field="com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS" />
        </array>
    </property>

    <property name="dateFormat">
        <bean class="java.text.SimpleDateFormat">
            <constructor-arg type="java.lang.String" value="yyyy-MM-dd"></constructor-arg>
        </bean>
    </property>

    <property name="modulesToInstall"
        value="
        com.fasterxml.jackson.datatype.jdk8.Jdk8Module,
    com.fasterxml.jackson.datatype.jsr310.JavaTimeModule,
    com.fasterxml.jackson.module.paramnames.ParameterNamesModule" />
</bean>

<bean id="jsonHttpMessageConverter"
    class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
    <property name="supportedMediaTypes" value="application/json" />
    <property name="objectMapper" ref="objectMapper" />
</bean>

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

This setup seemed to do the trick.

like image 173
tjholmes66 Avatar answered Jun 08 '26 03:06

tjholmes66