Spring 启动 kotlin jackson LocalDateTime 序列化程序
Spring boot kotlin jackson LocalDateTime serializer
我在 spring 启动时使用 kotlin。当我请求端点时,应该 return 我 LocalDateTime
对象,它 return 是我的数组而不是字符串。
示例:
"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 序列化程序,有什么处理方法吗?
我在我的一个配置 类 上删除 @EnableWebMvc
后问题得到解决。
我在 spring 启动时使用 kotlin。当我请求端点时,应该 return 我 LocalDateTime
对象,它 return 是我的数组而不是字符串。
示例:
"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 序列化程序,有什么处理方法吗?
我在我的一个配置 类 上删除 @EnableWebMvc
后问题得到解决。