19

Java 8 date/time type `java.time.LocalDateTime` not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" to enable handling

I have been getting this error in my spring boot project that uses Gradle. I added the below given Gradle dependency for jsr310 as well, still it didn't work.

implementation group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-jsr310', version: '2.13.3' 

How can I fix this error? My project uses Java 17 and Spring 2.6.7.

1
  • What did your search turn up? Commented Oct 25, 2022 at 4:16

2 Answers 2

22

JavaTimeModule should be registered explicitly:

@Configuration public class JacksonConfiguration { @Bean public ObjectMapper objectMapper() { ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new JavaTimeModule()); return mapper; } } 

UPDATE:

The first solution should be used with jackson-datatype-jsr310 versions 2.x before 2.9. Since you added version 2.13.3, the module should be registered as shown below, according to the answer.

@Configuration public class JacksonConfiguration { @Bean public ObjectMapper objectMapper() { return JsonMapper.builder() .addModule(new JavaTimeModule()) .build(); } } 

UPDATE 2:

Starting with Jackson 2.2, Modules can be automatically discovered using the Service Provider Interface (SPI) feature. You can activate this by instructing an ObjectMapper to find and register all Modules:

// Jackson 2.10 and later ObjectMapper mapper = JsonMapper.builder() .findAndAddModules() .build(); // or, 2.x before 2.9 ObjectMapper mapper = new ObjectMapper(); mapper.findAndRegisterModules(); 

For reference: jackson-modules-java8 - Registering modules

Sign up to request clarification or add additional context in comments.

8 Comments

This did not work either mate.
Are you still getting the same error, Java 8 date/time type java.time.LocalDateTime not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" to enable handling?
@Kalindu I didn't see the version of the added module in the first place. I added a solution that will work with 2.13.3.
So I'm using com.fasterxml.jackson.core:jackson-databind:2.13.4 && jackson-datatype-jsr310 version: '2.14.1' and the above isn't working for me. Any suggestions
@MetaCoder Have you tried the second solution? I also added a solution for automatically discovering and registering modules.
|
12
new ObjectMapper().registerModule(new JavaTimeModule()); 

Hope, this helps you.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.