【发布时间】:2021-03-30 11:46:40
【问题描述】:
我正在使用带有弹簧靴的 kotlin。当我请求端点时,它应该返回我 LocalDateTime 对象,它返回我数组而不是字符串。
例子:
"createdAt": [
2020,
12,
16,
23,
26,
10,
932070000
]
我的期望:
"createdAt": "2020-12-16 23:26:10"
我创建了Jackson2ObjectMapperBuilderCustomizer,但看起来它根本不起作用
@Configuration
class JacksonObjectMapperConfiguration {
companion object {
private const val dateFormat = "yyyy-MM-dd"
private const val dateTimeFormat = "yyyy-MM-dd HH:mm:ss"
}
@Bean fun jackson2ObjectMapperBuilderCustomizer(): Jackson2ObjectMapperBuilderCustomizer {
return Jackson2ObjectMapperBuilderCustomizer {
builder -> builder.simpleDateFormat(dateFormat)
builder.serializers(LocalDateSerializer(DateTimeFormatter.ofPattern(dateFormat)))
builder.serializers(LocalDateTimeSerializer(DateTimeFormatter.ofPattern(dateTimeFormat)))
}
}
@Bean
@Primary
fun objectMapper(): ObjectMapper =
ObjectMapper()
.registerModule(KotlinModule())
}
我的 build.gradle.kts 依赖于杰克逊
implementation(group="com.fasterxml.jackson.module", name="jackson-module-kotlin", version="2.11.3")
我想为我的应用程序中的所有字段创建全局 LocalDateTime 和 LocalDate 序列化程序,有什么想法可以处理吗?
【问题讨论】:
-
您还需要为 objectMapper 注册
JavaTimeModule。试试我的这个answer,虽然它是用 Java 编写的,但很相似。 -
@MadhuBhat 抱歉,但这没有帮助
标签: spring-boot kotlin jackson