Home > English, java, javascript, work > Return usable Joda dates in JSON with Jackson

Return usable Joda dates in JSON with Jackson

Every sane person ditches the Java Date-api and uses Joda. Okay, sometimes you run into trouble. This time when I was using the Spring @ResponseBody to return JSON and my date got returned as a big hairy monster (some Chronology and things which really scared me).
In this short article I’ll explain how to fix it. Since I try to be concise please feel free to ask me to elaborate.

Since Spring sometimes seems to bundle an older version of Jackson I will include my Jackson-dependency to the dependencies:


The important dependency is the last one. This gives you the class JodaModule, which does all the magic for you.

Now you have to create an ObjectMapper:

public class JodaObjectMapper extends ObjectMapper {

    public JodaObjectMapper() {
        registerModule(new JodaModule());

Now add to *-web-servlet.xml:

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

Now you’re almost done. In JSON the date will be returned as an array: [2011,10,5]

With new Date(array[0], array[1], array[2]) you will have a Date object in Javascript.

Categorieën:English, java, javascript, work
  1. Nog geen reacties
  1. No trackbacks yet.

Geef een reactie

Vul je gegevens in of klik op een icoon om in te loggen.

WordPress.com logo

Je reageert onder je WordPress.com account. Log uit / Bijwerken )


Je reageert onder je Twitter account. Log uit / Bijwerken )

Facebook foto

Je reageert onder je Facebook account. Log uit / Bijwerken )

Google+ photo

Je reageert onder je Google+ account. Log uit / Bijwerken )

Verbinden met %s


Ontvang elk nieuw bericht direct in je inbox.

%d bloggers op de volgende wijze: