如果格式化或将对象进行序列化时报错:
InvalidDefinitionException:
Java 8 date/time type `java.time.LocalDateTime` not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" to enable handling
解决方法:
1.先添加依赖
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.17.0</version>
</dependency>
2.在使用时设置时间日期格式化器
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
OBJECT_MAPPER.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
// 该 JavaTimeModule() 来自于上面引入的jackson-datatype-jsr310 依赖
OBJECT_MAPPER.registerModule(new JavaTimeModule());
参考:【常见问题】Java 8 date time type
java.time.LocalDateTime
not supported by default 日期序列化问题 - 知乎 (zhihu.com)