JDK8 java.time 是否有 jackson 数据类型模块? [英] Is there a jackson datatype module for JDK8 java.time?

查看:35
本文介绍了JDK8 java.time 是否有 jackson 数据类型模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找新的JDK8 java.time 类.我浏览了 FasterXML GitHub 项目列表,但目前没有找到.

据我所知,Jackson 仍在针对 JDK6 进行编译,因此不能直接使用这些类,并且必须将其构建为单独的模块,正如 Joda 所要求的那样.

我不介意开始这个项目,但我想看看是否还有其他工作正在进行中.

解决方案

如前所述,Jackson-数据类型-JSR310 提供对 Java 8 Time 的支持.

自 Jackson 2.6.0 以来,旧的"JSR310Module 已弃用.它被 JavaTimeModule 取代.Maven 依赖是相同的(您可以在 Maven Central 中找到当前版本):

<依赖><groupId>com.fasterxml.jackson.datatype</groupId><artifactId>jackson-datatype-jsr310</artifactId><version>2.6.0</version></依赖>

你必须像这样注册模块:

ObjectMapper mapper = new ObjectMapper();mapper.registerModule(new JavaTimeModule());

或者像这样:

ObjectMapper mapper = new ObjectMapper();mapper.findAndRegisterModules();

或者像这样(从 2.10 开始可能):

ObjectMapper mapper = JsonMapper.builder().findAndAddModules().建造();

<块引用>

请注意,从 2.6 开始,此模块不支持自动注册,因为存在旧版本 JSR310Module.旧版本具有相同的功能,但默认配置略有不同:请参阅 com.fasterxml.jackson.datatype.jsr310.JSR310Module 了解详情.

GitHub 上的 JavaTimeModule 源

JavaTimeModule 使用

I'm looking for a module for the new JDK8 java.time classes. I have looked through the FasterXML GitHub Project Listing and presently found none.

As I understand Jackson is still being compiled against JDK6 so can not use these classes directly and must have this built as a separate module, as was required with Joda.

I don't mind starting the project, though looking to see if any other efforts were already underway.

解决方案

As already mentioned, Jackson-Datatype-JSR310 provides support for Java 8 Time.

Since Jackson 2.6.0 the "old" JSR310Module is deprecated. It is replaced by JavaTimeModule. Maven dependency is the same (you can find the current version in Maven Central):

<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
    <version>2.6.0</version>
</dependency>

You have to register the module like this:

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());

Or like this:

ObjectMapper mapper = new ObjectMapper(); 
mapper.findAndRegisterModules();

Or like this (since 2.10 possible):

ObjectMapper mapper = JsonMapper.builder()
        .findAndAddModules()
        .build();

Note that as of 2.6, this module does NOT support auto-registration, because of existence of legacy version, JSR310Module. Legacy version has the same functionality, but slightly different default configuration: see com.fasterxml.jackson.datatype.jsr310.JSR310Module for details.

JavaTimeModule Source at GitHub

JavaTimeModule Usage

这篇关于JDK8 java.time 是否有 jackson 数据类型模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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