【问题标题】:Spring boot kotlin jackson LocalDateTime serializerSpring Boot kotlin jackson LocalDateTime 序列化器
【发布时间】: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


【解决方案1】:

在我的一个配置类中删除 @EnableWebMvc 后问题已解决。

【讨论】:

  • 对我来说,这是在我实现 WebFluxConfigurer 并提供 CORS 映射的 @Configuration 类上删除 @EnableWebFlux。谢谢你的提示。和你一样,我几乎尝试了所有自定义 json 序列化的方法。
猜你喜欢
  • 2019-02-16
  • 2021-01-19
  • 2019-04-21
  • 1970-01-01
  • 2017-09-09
  • 2019-04-03
  • 2018-11-17
  • 2021-03-31
  • 2018-07-24
相关资源
最近更新 更多