I am getting this error (note that we are talking about LocalDate, not LocalDateTime) :
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of java.time.LocalDate
(no Creators, like default construct, exist): no String-argument constructor/factory method to deserialize from String value ('2012-03-12')
I have been searching for this problem, and tried the "tricks" that are mentioned frequently in other answers, such as (I have been trying one by one of these tricks, plus I have been combining them in different ways) :
- Include com.fasterxml.jackson.datatype:jackson-datatype-jsr310
- Tell Jackson ObjectMapper to use JavaTimeModule by registering the module through a bean
- Try using jackson-module-kotlin
- Try more dependencies from Jackson (different combinations)
- Set write-dates-as-timestamps in the spring config to false
- Add @JsonCreator constructor to the data class
My dto data class looks like this :
data class DateDto (
@JsonProperty("date")
@JsonDeserialize(using = LocalDateDeserializer::class)
@JsonSerialize(using = LocalDateSerializer::class)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
val date : LocalDate
);
However, I have no luck with these tricks, and I don't understand what I am doing wrong.
Why is this so difficult ? Am I missing something very obvious ? I am using Spring Boot 2.2.7, Kotlin 1.3.70, jackson-datatype-jsr310 2.11.1.