Note that there are some explanatory texts on larger screens.

plurals
  1. POJackson automatic formatting of Joda DateTime to ISO 8601 format
    text
    copied!<p>According to <a href="http://wiki.fasterxml.com/JacksonFAQDateHandling" rel="noreferrer">http://wiki.fasterxml.com/JacksonFAQDateHandling</a>, “DateTime can be <strong>automatically</strong> serialized/deserialized similar to how java.util.Date is handled.” However, I am not able to accomplish this <strong>automatic</strong> functionality. There are StackOverflow discussions related to this topic yet most involve a code-based solution, but based upon the quote above I should be able to accomplish this via simple configuration.</p> <p>Per <a href="http://wiki.fasterxml.com/JacksonFAQDateHandling" rel="noreferrer">http://wiki.fasterxml.com/JacksonFAQDateHandling</a> I have my configuration set so that writing dates as timestamps is false. The result is that java.util.Date types are serialized to ISO 8601 format, but org.joda.time.DateTime types are serialized to a long object representation.</p> <p>My environment is this: </p> <p>Jackson 2.1<br> Joda time 2.1<br> Spring 3.2<br> Java 1.6 </p> <p>My Spring configuration for the jsonMapper bean is </p> <pre><code>@Bean public ObjectMapper jsonMapper() { ObjectMapper objectMapper = new ObjectMapper(); //Fully qualified path shows I am using latest enum ObjectMapper.configure(com.fasterxml.jackson.databind.SerializationFeature. WRITE_DATES_AS_TIMESTAMPS , false); return objectMapper; } </code></pre> <p>My test code snippet is this </p> <pre><code>Date d = new Date(); DateTime dt = new DateTime(d); //Joda time Map&lt;String, Object&gt; link = new LinkedHashMap&lt;String, Object&gt;(); link.put("date", d); link.put("createdDateTime", dt); </code></pre> <p>The resulting snippet of JSON output is this:</p> <pre><code>{"date":"2012-12-24T21:20:47.668+0000"} {"createdDateTime": {"year":2012,"dayOfMonth":24,"dayOfWeek":1,"era":1,"dayOfYear":359,"centuryOfEra":20,"yearOfEra":2012,"yearOfCentury":12,"weekyear":2012,"monthOfYear":12 *... remainder snipped for brevity*}} </code></pre> <p>My expectation is that the DateTime object should matche that of the Date object based upon the configuration. What am I doing wrong, or what am I misunderstanding? Am I reading too much into the word <strong>automatically</strong> from the Jackson documentation and the fact that a string representation was produced, albeit not ISO 8601, is producing the advertised automatic functionality?</p>
 

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