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?
CodePudding user response:
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.
CodePudding user response:
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.