java - 在 Spring Boot 2 中使用自定义 ObjectMapper 返回 ISO-8601 日期
问题描述
我希望我的 s从我的 Spring REST 控制器中LocalDateTime
作为 ISO-8601 字符串(例如 )返回。"2020-10-12T10:57:15Z"
这以前有效,但现在我使用的是自定义 Jackson2,ObjectMapper
这些日期改为作为数组返回:[2020, 10, 12, 10, 57, 15, 200000000]
。
为什么会发生这种情况?如何自定义ObjectMapper
同时仍返回 ISO-8601 日期?
解决方案
JacksonAutoConfiguration
创建一个关闭ObjectMapper
该特性的函数,它以 ISO-8601 字符串形式返回。当您提供自定义时,此默认自动配置将关闭。WRITE_DATES_AS_TIMESTAMPS
LocalDateTimes
ObjectMapper
这可以通过ObjectMapper
提供一个Jackson2ObjectMapperBuilderCustomizer
. 该 bean 将用于JacksonAutoConfiguration
自定义,ObjectMapper
同时保持自动配置的行为,例如关闭WRITE_DATES_AS_TIMESTAMPS
功能。
@Configuration
public class Config {
@Bean
public Jackson2ObjectMapperBuilderCustomizer objectMapperBuilderCustomizer() {
return jacksonObjectMapperBuilder -> {
// Customize the ObjectMapper while maintaining the auto-configuration
};
}
}
推荐阅读
- amazon-web-services - 捕捉错过的话语
- java - 如何递归删除未排序列表中小于给定 int 的所有元素?
- java - Java - 运算符不等于不在字符串的 if 条件下工作
- kotlin - 结构化并发和协程的“一劳永逸”
- unit-testing - Roboletric 无法使自定义对话框可见
- excel - 根据多列中的条件查找平均值
- mysql - 如何在一个联合中订购 2 个表?
- f# - 为什么这个使用 IndexOf 的函数总是返回 0?
- google-bigquery - 在 google Bigquery API 中出现间歇性 404 Not Found 错误
- html - 使用 CSS 用类型装饰单词