3

After updating my spring-boot-starter-parent version from 2.4.8 to 2.5.4, I started having this error with jackson serialization, when trying to deserialize a LocalDate:

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

To my knowledge, this shouldn't happen (and it doesn't on previous versions) since Spring Boot has those Jackson dependencies by default (jackson-datatype-jdk8, jackson-datatype-jsr310, etc)

I have no custom Jackson configurations.
Did anything change in the 2.5.x version of Spring boot?

Miguel Santos
  • 344
  • 3
  • 9
  • 1
    https://stackoverflow.com/questions/67874510/spring-boot-2-5-0-and-invaliddefinitionexception-java-8-date-time-type-java-ti Is it not the same problem? ;) – lolcio Sep 15 '21 at 10:43
  • Similar problem for Kotlin module: https://github.com/spring-projects/spring-framework/issues/21040. Solution is invoke findModulesViaServiceLoader method in configuration. Good luck! – lolcio Sep 15 '21 at 10:44
  • @lolcio you're right, this is a duplicate. I searched for a similar question but couldn't find it. Thanks! – Miguel Santos Sep 15 '21 at 11:07

2 Answers2

4

This problem occurs because JSON doesn't natively have a date format, so it represents dates as String.

The String representation of a date isn't the same as an object of type LocalDate in memory, so we need an external deserializer to read that field from a String, and a serializer to render the date to String format.

Have below dependency:-

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

Use the LocalDateDeserializer and JsonFormat annotations at the entity level.

public class EntityWithDate{

    @JsonDeserialize(using = LocalDateDeserializer.class)
    @JsonSerialize(using = LocalDateSerializer.class)
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy")
    public LocalDate operationDate;
}

One can also use Jackson's native support for serializing and deserializing dates.

N K Shukla
  • 288
  • 1
  • 3
  • 12
  • Spring Boot already has those dependencies of out the box. This question is actually a duplicate of https://stackoverflow.com/questions/67874510/spring-boot-2-5-0-and-invaliddefinitionexception-java-8-date-time-type-java-ti – Miguel Santos Sep 15 '21 at 11:07
2

This is a duplicate of Spring Boot 2.5.0 and InvalidDefinitionException: Java 8 date/time type `java.time.Instant` not supported by default

I don't think I have enough reputation to mark it as such.

Miguel Santos
  • 344
  • 3
  • 9