Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
265 views
in Technique[技术] by (71.8m points)

Is there a jackson datatype module for JDK8 java.time?

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.

question from:https://stackoverflow.com/questions/21384820/is-there-a-jackson-datatype-module-for-jdk8-java-time

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...