Solution for com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Java 8 date/time type


In this tutorials, We are going to learn fix for following errors in spring or java applications.

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Java 8 date/time type java.time.LocalDate not supported by default: add Module “com.fasterxml.jackson.datatype:jackson-datatype-jsr310” to enable handling

Java8 introduced many new classes for handling date and time in java.time package. This error will be thrown when you used below classes in pojo fields for json conversion.

  • LocalDateTime
  • Instant
  • LocalDate
  • All java8 classes

This tutorial covers for below things

  • Java 8 LocalDate Jackson mapper
  • Serialize and Deserialize Java 8 LocalDateTime

For example, In Spring application, Serialization and deserialization which means converting from pojo to/from json done by Jackson. This will be handle by ObjectMapper of Jackson implementation

If you are using java8 Date classes for pojo fields, the files are converted to JSON as below


{
	"dayOfYear":150,
	"month":"AUGUST",
	"dayOfWeek":"MONDAY",
	"nano":231713111,
	"year":2020,
	"monthValue":8,
	"dayOfMonth":24,

}

and expected value is “2020:08:24”

Instead of expected value it throws an com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Java 8 date/time type java.time.LocalDate not supported by default: add Module “com.fasterxml.jackson.datatyp e:jackson-datatype-jsr310” to enable handling

There are many ways of fixing this

 <dependency>
            <groupId>com.fasterxml.jackson.module</groupId>
            <artifactId>jackson-module-parameter-names</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jdk8</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jsr310</artifactId>
        </dependency>

Seriliazation jackson error

When you are serializing an object to json format, You have to configure the below things

With prorammatically,

  • First register the object mapper with JavaTimeModule module as follows
  • change WRITE_DATES_AS_TIMESTAMPS to false using jackson ObjectMapper
        ObjectMapper objectMapper = 
    new ObjectMapper().registerModule(new JavaTimeModule())
            .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);

The other way you can enable in application.properties

spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS: false

with application.yaml here are code changes

spring:
  jackson:
    serialization:
      WRITE_DATES_AS_TIMESTAMPS: false

This way, Serialization works perfect for java8 date and time classes

LocalDate DeSerialization jackson format error

if you are using localdate field in pojo, Spring can not able to convert java8 classes, So you have to add below code to serialize and deserialize localdate

import org.codehaus.jackson.map.annotate.JsonDeserialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.annotation.JsonFormat;
import java.time.LocalDate
class employee{

 @JsonDeserialize(using = LocalDateDeserializer.class)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd ")
private LocalDate created_date;
}

LocalDateTime deserialization jackson format error

LocalDateTime contains the data for date and time,

In pojo class, You have to add jackson annotation with date format string

import org.codehaus.jackson.map.annotate.JsonDeserialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.annotation.JsonFormat;
import java.time.LocalDate
class employee{

 @JsonDeserialize(using = LocalDateDeserializer.class)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDate created_date;
}
THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.