Jackson Error: Java 8 date/time type not supported by default

Learn to fix the error Java 8 date/time type not supported by default while serializing and deserializing Java 8 Date time classes using Jackson.

1. Problem

The error occurs when we serialize a Java object or deserialize JSON to POJO, and the POJO contains new Java 8 date time classes such as LocalDate, LocalTime, LocalDateTime etc.

For example, the following Employee class has LocalDate type field.

public class Employee {

  private Long id;
  private String name;
  private LocalDate dateOfBirth;
}

When we serialize an instance of this class, we get the following exception:

Exception in thread "main" java.lang.IllegalArgumentException: Java 8 date/time type `java.time.LocalDate` not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" to enable handling (through reference chain: com.howtodoinjava.core.objectToMap.Employee["dateOfBirth"])
	at com.fasterxml.jackson.databind.ObjectMapper ._convert(ObjectMapper.java:4393)
	at com.fasterxml.jackson.databind.ObjectMapper .convertValue(ObjectMapper.java:4324)
	at com.howtodoinjava.core.objectToMap .ObjectToMapUsingJackson.main(ObjectToMapUsingJackson.java:25)

2. Solution

We must add support to new Java 8 classes in two steps to fix this error.

First, add the latest version of com.fasterxml.jackson.datatype:jackson-datatype-jsr310 Maven dependency.

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

Second, register the module JavaTimeModule either with ObjectMapper or JsonMapper based on what you are using.

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

//or 

JsonMapper jsonMapper = new JsonMapper();
jsonMapper.registerModule(new JavaTimeModule());

After registering the JavaTimeModule, the above error will go away.

Happy Learning !!

Leave a Reply

0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial