Java Spring Jackons日期序列化
【腾讯云】亏本大甩卖,服务器4核16G 1年370元(带宽12M,系统盘120GB SSD盘,月流量2000GB)!!!!!!
云产品 配置 价格
服务器 1核2G,带宽5M,系统盘50GB SSD盘,月流量500GB 38元/年
MySQL 1核1G 19元/年
服务器 16核32G,带宽18M,系统盘250GB SSD盘,月流量5000GB 1197元/年
点我进入腾讯云,查看更多详情

I'm using Spring MVC and Jackson for JSON de/serialization. But im facing a problem with serializing a date.

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).

The code below is my spring config, but it does not work. It's still returning an epoch date.

So my question is, how can I serialize to a non-epoch date?

<!-- JSON -->
<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
    <property name="supportedMediaTypes" value="application/json" />
    <property name="objectMapper" ref="jacksonObjectMapper" />
</bean>

<bean id="jacksonObjectMapper" class="org.codehaus.jackson.map.ObjectMapper" />

<bean id="jacksonSerializationConfig" class="org.codehaus.jackson.map.SerializationConfig" factory-bean="jacksonObjectMapper" factory-method="getSerializationConfig" />

<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetObject" ref="jacksonSerializationConfig" />
    <property name="targetMethod" value="setSerializationInclusion" />
    <property name="arguments">
        <list>
            <value type="org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion">NON_NULL</value>
        </list>
    </property>
</bean>

<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetObject" ref="jacksonSerializationConfig" />
    <property name="targetMethod" value="setDateFormat" />
    <property name="arguments">
        <list>
            <value type="java.text.SimpleDateFormat">yyyy-MM-dd'T'HH:mm:ss.SSSZ</value>
        </list>
    </property>
</bean>

<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetObject" ref="jacksonSerializationConfig" />
    <property name="targetMethod" value="enable" />
    <property name="arguments">
        <list>
            <value type="org.codehaus.jackson.map.SerializationConfig.Feature">WRITE_DATES_AS_TIMESTAMPS</value>
        </list>
    </property>
</bean>

#0

Much simpler way to do this now in Spring 3.1.

public class CustomObjectMapper extends ObjectMapper {

    public CustomObjectMapper() {
        configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);            
        setDateFormat(new ISO8601DateFormat());
    }
}

And then register this as a bean and customize the mvc:annotation-driven element.

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

<bean id="customObjectMapper" class="CustomObjectMapper"/>

#1

I found the following to work with Spring Platform 2.0 (Spring 4.2) and Jackson 2.6.3

pom.xml

<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
</dependency>

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

applicationContext.xml

<mvc:annotation-driven>
  <mvc:message-converters>
     <beans:bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
       <beans:property name="objectMapper">
         <beans:bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
         <beans:property name="featuresToDisable">
           <beans:array>
             <util:constant static-field="com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS"/>
           </beans:array>
         </beans:property>               
         </beans:bean>
       </beans:property>
     </beans:bean>
   </mvc:message-converters>        
</mvc:annotation-driven>

#2

Solution using Spring 3.1.3 and Jackson 2.1.0 that works for me (based on Ryans answer and Kornys notes with additional change in Java code "SerializationConfig.Feature" -> "SerializationFeature")

public class DateObjectMapper extends ObjectMapper {

public DateObjectMapper() {
    configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);            
}

Configuration:

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

<bean id="dateObjectMapper" class="DateObjectMapper"/>

#3

I've fixed my problem. So for reference to others with a similar problem.

I've deleted the following elements in the spring context config xml:

<mvc:annotation-driven/>

and

<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject" ref="jacksonSerializationConfig" />
<property name="targetMethod" value="setDateFormat" />
<property name="arguments">
    <list>
        <value type="java.text.SimpleDateFormat">yyyy-MM-dd'T'HH:mm:ss.SSSZ</value>
    </list>
</property>

Then I added the following:

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <property name="order" value="1" />
</bean>

and changed the following:

<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject" ref="jacksonSerializationConfig" />
<property name="targetMethod" value="enable" />
<property name="arguments">
    <list>
        <value type="org.codehaus.jackson.map.SerializationConfig.Feature">WRITE_DATES_AS_TIMESTAMPS</value>
    </list>
</property>

in

<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject" ref="jacksonSerializationConfig" />
<property name="targetMethod" value="disable" />
<property name="arguments">
    <list>
        <value type="org.codehaus.jackson.map.SerializationConfig.Feature">WRITE_DATES_AS_TIMESTAMPS</value>
    </list>
</property>

After this Spring was able to serialize my date into json as a ISO date.

推荐文章

用于添加/删除行的jquery live函数

用于添加/删除行的jquery live函数

推荐文章

设置证书友好名称

设置证书友好名称

推荐文章

C: 头部内含物

C: 头部内含物

推荐文章

以编程方式识别iphone设备

以编程方式识别iphone设备

推荐文章

Rundll32加载顺序问题

Rundll32加载顺序问题

推荐文章

检查PHP变量的FireBug扩展

检查PHP变量的FireBug扩展

推荐文章

展开AS3中的变量参数

展开AS3中的变量参数

推荐文章

无法导出5000行

无法导出5000行

推荐文章

DOCTYPE的选择是否会影响javascript代码所看到的DOM?

DOCTYPE的选择是否会影响javascript代码所看到的DOM?

推荐文章

获取转义URL参数

获取转义URL参数

推荐文章

如何在C语言中实现类?

如何在C语言中实现类?

推荐文章

使用专有数据库系统构建应用程序的利弊

使用专有数据库系统构建应用程序的利弊

推荐文章

使用批处理文件删除dll

使用批处理文件删除dll

推荐文章

使用C#?防止在ListView中出现两个条目?

使用C#?防止在ListView中出现两个条目?

推荐文章

究竟什么是可视化的?

究竟什么是可视化的?

推荐文章

“加载操纵器时出错”

“加载操纵器时出错”