1

我正在尝试jackson-modules-java8使用 Ktor 和 Jackson 进行配置,但无济于事。

该模块被添加到gradle.build

dependencies {
    ...
    implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.12.0-rc2'
    ...
}

根据杰克逊文档,我应该这样做:

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?
    }
}
4

1 回答 1

1

根据官方示例,如果您想添加一个可以使用的模块

registerModule

像这样:

install(ContentNegotiation) {
    jackson {
        configure(SerializationFeature.INDENT_OUTPUT, true)
        setDefaultPrettyPrinter(DefaultPrettyPrinter().apply {
            indentArraysWith(DefaultPrettyPrinter.FixedSpaceIndenter.instance)
            indentObjectsWith(DefaultIndenter("  ", "\n"))
        })
        registerModule(JavaTimeModule())  // support java.time.* types
    }
}
于 2020-11-18T21:17:28.827 回答