Java Spring Jackson日期序列化

时间:2011-10-06 09:05:02

标签: spring serialization jackson

我正在使用Spring MVC和Jackson进行JSON de / serialization。但我面临着序列化日期的问题。

默认情况下,杰克逊将日期序列化为时代。但我想将其序列化为ISO日期(即06-10-2011 11:00:00)。

下面的代码是我的spring配置,但它不起作用。它仍然返回一个纪元日期。

所以我的问题是,如何序列化为非纪元日期?

<!-- 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>

4 个答案:

答案 0 :(得分:17)

现在在Spring 3.1中更简单的方法。

public class CustomObjectMapper extends ObjectMapper {

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

然后将其注册为bean并自定义mvc:annotation-driven元素。

<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 :(得分:5)

使用适用于我的Spring 3.1.3和Jackson 2.1.0的解决方案(基于Ryans答案和Kornys笔记以及Java代码“SerializationConfig.Feature”中的其他更改 - &gt;“SerializationFeature”)

public class DateObjectMapper extends ObjectMapper {

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

配置:

<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"/>

答案 2 :(得分:5)

我发现以下内容适用于Spring Platform 2.0(Spring 4.2)和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>

答案 3 :(得分:1)

我已经解决了我的问题。所以为了引用其他有类似问题的人。

我在spring context config xml中删除了以下元素:

<mvc:annotation-driven/>

<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 class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <property name="order" value="1" />
</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 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>

此Spring之后能够将我的日期序列化为json作为ISO日期。