杰克逊自动格式化Joda DateTime为ISO 8601格式

Jef*_*f M 36 spring json jodatime jackson

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

根据http://wiki.fasterxml.com/JacksonFAQDateHandling我有我的配置集,以便将日期写为时间戳为false.结果是java.util.Date类型被序列化为ISO 8601格式,但org.joda.time.DateTime类型被序列化为长对象表示.

我的环境是这样的:

Jackson 2.1
Joda时间2.1
Spring 3.2
Java 1.6

我的jsonMapper bean的Spring配置是

@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;
}
Run Code Online (Sandbox Code Playgroud)

我的测试代码片段是这样的

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);
Run Code Online (Sandbox Code Playgroud)

生成的JSON输出片段是这样的:

{"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*}}
Run Code Online (Sandbox Code Playgroud)

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

Jef*_*f M 48

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

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

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

<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-joda</artifactId>
    <version>${jackson.version}</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

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

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JodaModule());
objectMapper.configure(com.fasterxml.jackson.databind.SerializationFeature.
    WRITE_DATES_AS_TIMESTAMPS , false);
Run Code Online (Sandbox Code Playgroud)

  • 你把这个代码放在哪里?因为我的Spring 4.0.1似乎使用了不同的ObjectMapper实例(在AllEncompassingFormHttpMessageConverter中创建,请参阅新的MappingJackson2HttpMessageConverter) (10认同)
  • 如果你要从Spring使用`Jackson2ObjectMapperFactoryBean`,你就不需要明确地注册`JodaModule`.它在内部使用`Jackson2ObjectMapperBuilder`,如果它在类路径上可用,它会自动注册该模块. (2认同)
  • 很可能你还需要添加`objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz"));`如http://stackoverflow.com/questions/3269459/中所述如何对序列化 - 乔达-日期时间与 - 杰克逊JSON-processer/15605404#comment17457955_10835334 (2认同)

ker*_*vin 9

使用Spring Boot.

添加到您的Maven配置...

<dependency>
  <groupId>com.fasterxml.jackson.datatype</groupId>
  <artifactId>jackson-datatype-joda</artifactId>
  <version>2.7.5</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

然后到你的WebConfiguration ......

@Configuration
public class WebConfiguration extends WebMvcConfigurerAdapter
{
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters)
    {
      final MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
      final ObjectMapper objectMapper = new ObjectMapper();

      //configure Joda serialization
      objectMapper.registerModule(new JodaModule());
      objectMapper.configure(
          com.fasterxml.jackson.databind.SerializationFeature.
            WRITE_DATES_AS_TIMESTAMPS , false);

      // Other options such as how to deal with nulls or identing...
      objectMapper.setSerializationInclusion (
         JsonInclude.Include.NON_NULL);
      objectMapper.enable(SerializationFeature.INDENT_OUTPUT);

      converter.setObjectMapper(objectMapper);
      converters.add(converter);
      super.configureMessageConverters(converters);
    }
}
Run Code Online (Sandbox Code Playgroud)


Igo*_*hin 5

在 Spring Boot 中,配置更加简单。您只需声明 Maven 依赖项

    <dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-joda</artifactId>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

然后将配置参数添加到您的 application.yml/properties 文件中:

spring.jackson.serialization.write-dates-as-timestamps: false
Run Code Online (Sandbox Code Playgroud)