Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Register ObjectMapper in Spring 4.1.3 to serialize Joda DateTime

I'm trying to configure ObjectMapper in my Web application to serialize/deserialise dates presented as Joda's DateTime in ISO 8601 format. I found useful library jackson-datatype-joda and it's module JodaModule so I've added dependency:

<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-joda</artifactId>
    <version>2.4.4</version>
</dependency>

My Spring configuration:

<bean id="objectMapper"
    class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean"
    p:indentOutput="true">

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

    <property name="modulesToInstall" value="com.fasterxml.jackson.datatype.joda.JodaModule" /> 
</bean>

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

When I try to serialize this bean

public class Bean {
    private DateTime start = new DateTime();

    public DateTime getStart() { return start; }
    public void setStart(DateTime start) { this.start = start; }        
}

I get following output as long but instead want it to be in ISO 8601 format:

{"start":1418337158933}

I found that JodaModule also preloads if it is found in classpath so it is not neccessary to register it manually (see github repo) but this code is invoked many times during application start.

I think the reason is that ObjectMapper is instantiated in some other place.

UPDATE: The problem was that there was one more file with Spring configuration where ObjectMapper was declared. Both solutions given in answers will work. Cheers!

like image 201
rand0m86 Avatar asked Dec 11 '14 22:12

rand0m86


2 Answers

If you create you custom ObjectMapper class like in the code below it will override default objectMapper and you don't need your xml configuration at all:

@Service
public class CustomObjectMapper extends ObjectMapper {
    public CustomObjectMapper() {
        this.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        this.registerModule(new JodaModule());
    }
}

I've added this simple service to the simple spring boot application and got time in the expected format.

like image 119
Ilya Ovesnov Avatar answered Nov 14 '22 21:11

Ilya Ovesnov


If you use Java configuration, it can be like this:

@Configuration
public class JodaDateTimeConfig {

@Bean
public ObjectMapper getObjectMapper() {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerModule(new JodaModule());
    objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    return objectMapper;
}
like image 37
sendon1982 Avatar answered Nov 14 '22 21:11

sendon1982