Note that there are some explanatory texts on larger screens.

plurals
  1. POjackson2 JSON ISO 8601 date from JodaTime in Spring 3.2RC1
    text
    copied!<p>I'm working on a REST API in Spring MVC 3.2RC1. </p> <p>I'm fetching a JPA entity with a org.joda.time.DateTime timestamp in it and let Spring serialise it into JSON using</p> <pre><code>@RequestMapping(value = "/foobar", method = RequestMethod.GET, produces = "application/json") @ResponseBody </code></pre> <p>Using the default Jackson2 settings in Spring as I've only added </p> <pre><code>&lt;dependency&gt; &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt; &lt;artifactId&gt;jackson-annotations&lt;/artifactId&gt; &lt;version&gt;2.1.1&lt;/version&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt; &lt;artifactId&gt;jackson-core&lt;/artifactId&gt; &lt;version&gt;2.1.1&lt;/version&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt; &lt;artifactId&gt;jackson-databind&lt;/artifactId&gt; &lt;version&gt;2.1.1&lt;/version&gt; &lt;/dependency&gt; </code></pre> <p>to my POM and let Spring wire it up itself.</p> <p>The controller is generating:</p> <pre><code>"created":{"year":2012,"dayOfMonth":30,"dayOfWeek":5,"era":1,"dayOfYear":335,"weekOfWeekyear":48,"weekyear":2012,"monthOfYear":11,"yearOfEra":2012,"yearOfCentury":12,"centuryOfEra":20,"millisOfSecond":39,"millisOfDay":52684039,"secondOfMinute":4,"secondOfDay":52684,"minuteOfHour":38,"minuteOfDay":878,"hourOfDay":14,"millis":1354282684039,"zone":{"uncachedZone":{"cachable":true,"fixed":false,"id":"Europe/Stockholm"},"fixed":false,"id":"Europe/Stockholm"},"chronology":{"zone":{"uncachedZone":{"cachable":true,"fixed":false,"id":"Europe/Stockholm"},"fixed":false,"id":"Europe/Stockholm"}},"afterNow":false,"beforeNow":true,"equalNow":false} </code></pre> <p>But I would like it to be and ISO8601 date such as 2007-11-16T20:14:06.3Z (or with the offset).</p> <p>My guess is that I need to access the ObjectMapper and set mapper.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); But how do I get access to the ObjectMapper when using </p> <pre><code>&lt;mvc:annotation-driven /&gt; </code></pre> <p>P.S. I'm persisting the objects to PostgreSQL with JPA/Hibernate4 using UserType to get JodaTime support. D.S.</p> <p><strong>Update</strong></p> <p>The config below solves it for java.util.Date but still no dice for JodaTime.</p> <pre><code>&lt;annotation-driven&gt; &lt;message-converters&gt; &lt;beans:bean class="org.springframework.http.converter.StringHttpMessageConverter" /&gt; &lt;beans:bean class="org.springframework.http.converter.ResourceHttpMessageConverter" /&gt; &lt;beans:bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"&gt; &lt;beans:property name="objectMapper"&gt; &lt;beans:bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean" p:indentOutput="true" p:simpleDateFormat="yyyy-MM-dd'T'HH:mm:ss.SSSZ"&gt; &lt;/beans:bean&gt; &lt;/beans:property&gt; &lt;/beans:bean&gt; &lt;/message-converters&gt; &lt;/annotation-driven&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload