带有MappingJackson2HttpMessageConverter的RequestMappingHandlerAdapter在日期格式中不能正常工作

时间:2017-06-28 03:09:40

标签: json date jackson simpledateformat spring-4

我用spring4
我为jackson2HttpMessageConverter配置了ObjectMapper的simpleDateFormat和timezone属性,但是效果不好;

spring.xml:

   <context:annotation-config/>
<tx:annotation-driven transaction-manager="transactionManager"/>
<mvc:annotation-driven/>
<context:component-scan base-package="com.xxxxxxxxxxxxxx"/>

<bean id="timeZone" class="java.util.TimeZone" factory-method="getDefault"></bean>
<!--&lt;!&ndash;Jackson - objectMapper format date setting &ndash;&gt;-->
<bean id="objectMapperFactoryBean"
      class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean"
      p:indentOutput="true" p:simpleDateFormat="yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" p:timeZone-ref="timeZone">
</bean>
<bean id="jackson2HttpMessageConverter"
      class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
    <property name="supportedMediaTypes">
        <list>
            <value>text/html;charset=UTF-8</value>
            <value>text/json;charset=UTF-8</value>
            <value>application/json;charset=UTF-8</value>
        </list>
    </property>
    <property name="objectMapper" ref="objectMapperFactoryBean"/>
</bean>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="jackson2HttpMessageConverter"/>
        </list>
    </property>
</bean>

RequestBody json:

{
"secKillPromotionVo": {
    "type": 20,
    "name": "test4-xxxx",
    "showTime": "2017-06-16T16:25:41.000Z",
    "startTime": "2017-06-18T00:00:41.000Z",
    "endTime": "2017-06-18T23:55:41.000Z",
    "activityId": null,
    "email": "yinyuxia@le.com",
    "platform": "LeWin",
    "shopCode": "1",
    "shopName": "xxxxx",
    "createdUserId": "112424",
    "createdUser": xxxx"
},
"secKillProductList": [
    {
        "productId": "H04011000088",
        "productName": "xxxx",
        "productType": 1,
        "originPrice": 399900,
        "promotionPrice": 299900,
        "promotionStock": 1000,
        "maxNumPerUser": 1
    }
]
}

调试屏幕: enter image description here

和Response json:

{
"status": 1,
"msg": "successful",
"data": {
    "id": 2753,
    "type": 20,
    "name": "test4-XXXXX",
    "showTime": 1497630341000,
    "startTime": 1497744041000,
    "endTime": 1497830141000,
    "status": 0,
    "activityId": null,
    "createdUserId": "1234",
    "createdUser": "XXX",
    "email": "XXXXX",
    "platform": "XXXX",
    "shopCode": "1",
    "shopName": "XXXX"
}
}

ObjectMapper不能正常工作,out Date字段是使用默认设置序列化的。

现在我只为所有日期字段添加@JsonFormat注释来解决此问题,但是有人可以帮我解决它吗?

带有@JsonFormat注释的POJO:

public class SecKillPromotionVO {
private Integer id;
@ValidPromotionType(value = {PromotionType.secKill})
private Byte type;
@NotBlank
private String name;
@NotNull
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",timezone = "GMT+8")
private Date showTime;
@NotNull
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",timezone = "GMT+8")
private Date startTime;
@NotNull
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",timezone = "GMT+8")
private Date endTime;
private Byte status;
private String activityId;
private String createdUserId;
private String createdUser;

非常感谢!

0 个答案:

没有答案