Solution for com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Java 8 date/time type
In these tutorials, We are going to learn the fix for the 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 the 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 the Spring application, Serialization and deserialization mean converting from POJO to/from json done by Jackson. This will be handled 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 perfectly 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 a 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;
}