Java Spring Jackons Date serialization

后端 未结 4 1128
野的像风
野的像风 2021-01-13 00:20

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

相关标签:
4条回答
  • 2021-01-13 00:44

    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"/>
    
    0 讨论(0)
  • 2021-01-13 00:44

    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.

    0 讨论(0)
  • 2021-01-13 00:55

    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>
    
    0 讨论(0)
  • 2021-01-13 01:04

    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"/>
    
    0 讨论(0)
提交回复
热议问题