我正在尝试使用 Ktor 和 Jackson 配置 jackson-modules-java8
但无济于事。
该模块已添加到 gradle.build
dependencies {
...
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.12.0-rc2'
...
}
根据 jackson 文档,我应该这样做:
ObjectMapper mapper = JsonMapper.builder()
.addModule(new JavaTimeModule())
.build();
但在 Ktor 中我只能这样做:
install(ContentNegotiation) {
jackson {
// `this` is the ObjectMapper
this.enable(SerializationFeature.INDENT_OUTPUT)
// what to do here?
}
}
最佳答案
根据official example如果你想添加一个模块,你可以使用
注册模块
如下:
install(ContentNegotiation) {
jackson {
configure(SerializationFeature.INDENT_OUTPUT, true)
setDefaultPrettyPrinter(DefaultPrettyPrinter().apply {
indentArraysWith(DefaultPrettyPrinter.FixedSpaceIndenter.instance)
indentObjectsWith(DefaultIndenter(" ", "\n"))
})
registerModule(JavaTimeModule()) // support java.time.* types
}
}
关于jackson - 如何在 Ktor 中配置 jackson-modules-java8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64894340/