当我将 Spring Boot 应用程序从 2.4.1 升级到 2.6.1 时。我收到以下错误。
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Java 8 date/time type
java.time.Instant
not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" to enable handling (through reference chain: org.springframework.boot.actuate.trace.http.HttpTrace["timestamp"])
最佳答案
从 Spring Boot 2.4 升级到 2.6 后,您还将 Jackson 从 2.11 升级到 2.13。 Jackson 2.12 包含以下更改:https://github.com/FasterXML/jackson-databind/issues/2683
这会产生您所看到的错误消息,因为如果使用相应的日期时间,JSR 310 模块现在是必需的。
只有当您配置自己的 ObjectMapper
以使自动配置的对象退出时,您才会受到此影响。在配置 ObjectMapper
时,您需要显式添加 JSR 310 模块。这可以通过如下代码来完成:
objectMapper.registerModule(new JavaTimeModule());
github 上还有一个关于此问题的有点冗长但富有洞察力的讨论:https://github.com/spring-projects/spring-boot/issues/26859
关于java - 将 Spring Boot 从 2.4.1 升级到 2.6.1,出现 `java.time.Instant` 默认不支持错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70432809/