Spring boot Spring boot kotlin jackson LocalDateTime序列化程序
我在用kotlin和spring boot。当我请求端点时,它应该返回我Spring boot Spring boot kotlin jackson LocalDateTime序列化程序,spring-boot,kotlin,jackson,Spring Boot,Kotlin,Jackson,我在用kotlin和spring boot。当我请求端点时,它应该返回我LocalDateTime对象,它返回我数组而不是字符串 例如: "createdAt": [ 2020, 12, 16, 23, 26, 10, 932070000 ] 我所期望的是: "createdAt&q
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
与jackson相关
implementation(group="com.fasterxml.jackson.module", name="jackson-module-kotlin", version="2.11.3")
我想为我的应用程序中的所有字段创建全局LocalDateTime和LocalDate序列化程序,有什么办法吗?在我删除了一个配置类上的
@EnableWebMvc
后,问题得到了解决。您还需要为objectMapper注册JavaTimeModule
。试试我的这个,虽然它是Java的,但类似。@Madhubat抱歉,但这对我没有帮助,它是在一个@Configuration
类上删除@EnableWebFlux
,我在该类中实现webfluxconfigure
并提供CORS映射。谢谢你的提示。和您一样,我尝试了几乎所有定制json序列化的方法,但都没有成功。