Spring Controller + Jackson + Joda日期时间:如何维护Timezone?

时间:2014-01-29 19:23:59

标签: json spring-mvc timezone jackson jodatime

我的配置

Spring 3.2.5。发布

Jackson 2.3.0:

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.3.0</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.3.0</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-joda</artifactId>
        <version>2.3.0</version>
    </dependency>

Joda Time 2.3:

<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.3</version>
</dependency>

我使用了这个全球Spring MVC全局设置:

<!--Jackson - objectMapper format date setting -->
<beans:bean id="objectMapper"
    class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean"
    p:indentOutput="true" p:simpleDateFormat="yyyy-MM-dd'T'HH:mm:ss.SSSZ">
</beans:bean>

<!--Jackson - registerModule JodaModule -->
<beans:bean
    class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"
    p:targetObject-ref="objectMapper" p:targetMethod="registerModule">
    <beans:property name="arguments">
        <beans:list>
            <beans:bean class="com.fasterxml.jackson.datatype.joda.JodaModule" />
        </beans:list>
    </beans:property>
</beans:bean>

<!-- Spring MVC:  setting objectMapper in converter MappingJackson2HttpMessageConverter-->
<mvc:annotation-driven>
    <mvc:message-converters>
        <beans:bean
            class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <beans:property name="objectMapper" ref="objectMapper" />
        </beans:bean>
    </mvc:message-converters>
</mvc:annotation-driven>

我的问题:

杰克逊(我想..)用Z覆盖时区+8:00

事实上,如果我发送一个日期 +8:00 时区的JSON请求:

{
    "date": "2014-01-28T23:00:00.000+08:00"
}

....请求由经典的Spring MVC @Controller处理:

public JsonResponse insert(@RequestBody BeanDto beanWithDate)

BeanDto有“DateTime date”,属性。

BeanDto的属性日期具有以下值:

date = 2014-01-28T23:00:00.000Z

我的问题:

如何维护json请求的时区?

另一个小问题: 在spring mvc配置文件中是否有更智能的管理dateTime格式的解决方案?

谢谢你, 的Alessandro

0 个答案:

没有答案