杰克逊自动格式化Joda DateTime为ISO 8601格式 [英] Jackson automatic formatting of Joda DateTime to ISO 8601 format

查看:130
本文介绍了杰克逊自动格式化Joda DateTime为ISO 8601格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 http://wiki.fasterxml.com/JacksonFAQDateHandling ,DateTime可以自动序列化/反序列化,类似于java.util.Date的处理方式。但是,我无法完成此自动功能。有关于此主题的StackOverflow讨论,但大多数涉及基于代码的解决方案,但基于上面的引用,我应该能够通过简单的配置来实现这一点。

According to http://wiki.fasterxml.com/JacksonFAQDateHandling, "DateTime can be automatically serialized/deserialized similar to how java.util.Date is handled." However, I am not able to accomplish this automatic functionality. There are StackOverflow discussions related to this topic yet most involve a code-based solution, but based upon the quote above I should be able to accomplish this via simple configuration.

Per http://wiki.fasterxml.com/JacksonFAQDateHandling 我的配置已设置为将日期写为时间戳为假。结果是java.util.Date类型被序列化为ISO 8601格式,但org.joda.time.DateTime类型被序列化为长对象表示。

Per http://wiki.fasterxml.com/JacksonFAQDateHandling I have my configuration set so that writing dates as timestamps is false. The result is that java.util.Date types are serialized to ISO 8601 format, but org.joda.time.DateTime types are serialized to a long object representation.

My环境是这样的:

Jackson 2.1

Joda time 2.1

Spring 3.2

Java 1.6

Jackson 2.1
Joda time 2.1
Spring 3.2
Java 1.6

我的jsonMapper bean的Spring配置是

My Spring configuration for the jsonMapper bean is

@Bean
public ObjectMapper jsonMapper() {
    ObjectMapper objectMapper = new ObjectMapper();

    //Fully qualified path shows I am using latest enum
    ObjectMapper.configure(com.fasterxml.jackson.databind.SerializationFeature.
        WRITE_DATES_AS_TIMESTAMPS , false);

    return objectMapper;
}

我的测试代码片段是这个

My test code snippet is this

Date d = new Date();
DateTime dt = new DateTime(d); //Joda time 
Map<String, Object> link = new LinkedHashMap<String, Object>();
link.put("date", d);
link.put("createdDateTime", dt);

生成的JSON输出片段为:

The resulting snippet of JSON output is this:

{"date":"2012-12-24T21:20:47.668+0000"}

{"createdDateTime": {"year":2012,"dayOfMonth":24,"dayOfWeek":1,"era":1,"dayOfYear":359,"centuryOfEra":20,"yearOfEra":2012,"yearOfCentury":12,"weekyear":2012,"monthOfYear":12 *... remainder snipped for brevity*}}

我的期望是DateTime对象应该根据配置匹配Date对象的那个。我做错了什么,或者我误解了什么?我是否对Jackson文档中的自动这个词进行了过多的阅读,并且生成字符串表示虽然不是ISO 8601,却产生了广告自动功能?

My expectation is that the DateTime object should matche that of the Date object based upon the configuration. What am I doing wrong, or what am I misunderstanding? Am I reading too much into the word automatically from the Jackson documentation and the fact that a string representation was produced, albeit not ISO 8601, is producing the advertised automatic functionality?

推荐答案

我能够从杰克逊用户邮件列表中得到答案,并希望与您分享,因为这是一个新手问题。从阅读杰克逊日期常见问题解答,我没有意识到需要额外的依赖和注册,但事实就是这样。它在git hub项目页面上有记录, https://github.com/FasterXML/jackson-datatype-joda

I was able to get the answer to this from the Jackson user mailing list, and wanted to share with you since it is a newbie issue. From reading the Jackson Date FAQ, I did not realize that extra dependencies and registration are required, but that is the case. It is documented at the git hub project page here https://github.com/FasterXML/jackson-datatype-joda

基本上,我必须为特定于Joda数据类型的Jackson jar添加另一个依赖项,然后我必须在对象映射器上注册该模块的使用。代码片段如下。

Essentially, I had to add another dependency to a Jackson jar specific to the Joda data type, and then I had to register the use of that module on the object mapper. The code snippets are below.

对于我的Jackson Joda数据类型Maven依赖设置,我使用了这个:

For my Jackson Joda data type Maven dependency setup I used this:

<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-joda</artifactId>
    <version>${jackson.version}</version>
</dependency>

要注册Joda序列化/反序列化功能,我使用了这个:

To register the Joda serialization/deserialization feature I used this:

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JodaModule());
objectMapper.configure(com.fasterxml.jackson.databind.SerializationFeature.
    WRITE_DATES_AS_TIMESTAMPS , false);

这篇关于杰克逊自动格式化Joda DateTime为ISO 8601格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆