Spring Boot 2.5.0 and InvalidDefinitionException: Java 8 date/time type `java.time.Instant` not supported by default
Solution 1:
I saw an issue in one of my test classes. The problem there was it was creating a new ObjectMapper instance that was not adding the JavaTimeModule.
Here is a sample test that works in Spring 2.4.5 but fails in 2.5.0/2.5.1 with com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Java 8 date/time type java.time.ZonedDateTime
not supported by default
It might be due to the upgrade in the jackson-datatype-jsr310 version
package net.jpmchase.gti.gtfabric;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import java.time.ZonedDateTime;
public class ObjectMapperTest {
@Test
public void objectMapperTest() throws Exception{
ZonedDateTime time = ZonedDateTime.now();
ObjectMapper o = new ObjectMapper();
o.writeValueAsString(time);
}
}
To fix this particular test case had to add an explicit
ObjectMapper o = new ObjectMapper();
o.registerModule(new JavaTimeModule());
Solution 2:
Perhaps you are not using in your code Object Mapper provided by spring.
Wrong way:
ObjectMapper o = new ObjectMapper();
Correct way:
@Autowired
Jackson2ObjectMapperBuilder mapperBuilder;
...
ObjectMapper mapper = mapperBuilder.build();
Solution 3:
In case you are using Spring Data Couchbase then this might be your problem: https://github.com/spring-projects/spring-data-couchbase/blame/4.2.x/src/main/java/org/springframework/data/couchbase/config/AbstractCouchbaseConfiguration.java#L309
The bug report is here: https://github.com/spring-projects/spring-data-couchbase/issues/1209
This has been fixed in Spring-Data-Couchbase 4.3