Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Spring Jackons Date serialization
    primarykey
    data
    text
    <p>I'm using Spring MVC and Jackson for JSON de/serialization. But im facing a problem with serializing a date.</p> <p>By default Jackson serialize a date as an epoch. But i want to serialize it as a ISO date (i.e. 06-10-2011 11:00:00).</p> <p>The code below is my spring config, but it does not work. It's still returning an epoch date. </p> <p>So my question is, how can I serialize to a non-epoch date?</p> <pre><code>&lt;!-- JSON --&gt; &lt;bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"&gt; &lt;property name="supportedMediaTypes" value="application/json" /&gt; &lt;property name="objectMapper" ref="jacksonObjectMapper" /&gt; &lt;/bean&gt; &lt;bean id="jacksonObjectMapper" class="org.codehaus.jackson.map.ObjectMapper" /&gt; &lt;bean id="jacksonSerializationConfig" class="org.codehaus.jackson.map.SerializationConfig" factory-bean="jacksonObjectMapper" factory-method="getSerializationConfig" /&gt; &lt;bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"&gt; &lt;property name="targetObject" ref="jacksonSerializationConfig" /&gt; &lt;property name="targetMethod" value="setSerializationInclusion" /&gt; &lt;property name="arguments"&gt; &lt;list&gt; &lt;value type="org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion"&gt;NON_NULL&lt;/value&gt; &lt;/list&gt; &lt;/property&gt; &lt;/bean&gt; &lt;bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"&gt; &lt;property name="targetObject" ref="jacksonSerializationConfig" /&gt; &lt;property name="targetMethod" value="setDateFormat" /&gt; &lt;property name="arguments"&gt; &lt;list&gt; &lt;value type="java.text.SimpleDateFormat"&gt;yyyy-MM-dd'T'HH:mm:ss.SSSZ&lt;/value&gt; &lt;/list&gt; &lt;/property&gt; &lt;/bean&gt; &lt;bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"&gt; &lt;property name="targetObject" ref="jacksonSerializationConfig" /&gt; &lt;property name="targetMethod" value="enable" /&gt; &lt;property name="arguments"&gt; &lt;list&gt; &lt;value type="org.codehaus.jackson.map.SerializationConfig.Feature"&gt;WRITE_DATES_AS_TIMESTAMPS&lt;/value&gt; &lt;/list&gt; &lt;/property&gt; &lt;/bean&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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