Search code examples
javaspring-mvcjacksondate-format

Jackson 1.9 configuration for spring 3.1 (to globally set dateformat)


I successfully use Spring 3.1 mvc to produce rest webservices using json as http message. Until now I was setting on every field in my beans the notation to use a custom serializer/deserializer and ask jackson to format my date in a specific format. Now I would like to delete this notation syntax, and set a global dateformat. This is how i made it

this is my servlet configuration

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

<context:component-scan base-package="com.test.endpoints" />

<mvc:annotation-driven>
    <mvc:message-converters register-defaults="false">
        <bean
            class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
            <property name="objectMapper">
                <bean class="com.test.CustomObjectMapper" />
            </property>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

</beans>

this is the class CustomObjectMapper

package com.test;

import java.text.SimpleDateFormat;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig.Feature;

public class CustomObjectMapper extends ObjectMapper {

    public CustomObjectMapper() {
        super();
        configure(Feature.WRITE_DATES_AS_TIMESTAMPS, false);
        setDateFormat(new SimpleDateFormat(
                "EEE MMM dd yyyy HH:mm:ss 'GMT'ZZZ (z)"));
    }
}

Solution

  • Add below xml to mvc configuration to override date serialization globally.

    <mvc:annotation-driven > 
        <mvc:message-converters register-defaults="false">
            <bean     class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" >
                <property name="objectMapper">
                    <bean class="package.CustomObjectMapper"/>
                </property>
            </bean>
        </mvc:message-converters>
     </mvc:annotation-driven>
    

    and modify the CustomObjectMapper class like below.

    import org.codehaus.jackson.map.ObjectMapper;
    import org.codehaus.jackson.map.SerializationConfig.Feature;
    
    public class CustomObjectMapper extends ObjectMapper {
        public CustomObjectMapper(){
            super.configure(Feature.WRITE_DATES_AS_TIMESTAMPS, false);
        }
    }