3
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 dependancy 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. Thanks!

Kalindu
  • 111
  • 2
  • 9

2 Answers2

5

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

Toni
  • 3,296
  • 2
  • 13
  • 34
  • This did not work either mate. – Kalindu Oct 25 '22 at 06:04
  • 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`? – Ole V.V. Oct 25 '22 at 07:22
  • @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`. – Toni Oct 25 '22 at 08:42
  • 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 Dec 07 '22 at 09:31
  • @MetaCoder Have you tried the second solution? I also added a solution for automatically discovering and registering modules. – Toni Dec 07 '22 at 10:14
  • Thanks for coming back to me @birca123 do you mean update 2 when you say the second solution? – MetaCoder Dec 07 '22 at 10:23
  • I've tried this: ```@AutoConfiguration(before = JacksonAutoConfiguration.class) public class JacksonConfiguration { @Bean public ObjectMapper objectMapper() { ObjectMapper mapper = JsonMapper.builder() .findAndAddModules() .build(); return mapper; } } ``` – MetaCoder Dec 07 '22 at 10:28
  • Replace `@AutoConfiguration(before = JacksonAutoConfiguration.class)` with `@Configuration` and use either code you pasted or code from the "UPDATE" section. – Toni Dec 07 '22 at 10:49
1
new ObjectMapper().registerModule(new JavaTimeModule());

Hope, this helps you.