json - 使用 WebFlux 从资源中读取和解析文件的 react 方式?

标签 json kotlin spring-webflux project-reactor

我想知道从资源中读取、解析和提供文件的正确方法是什么。

目前,我做这样的事情:

fun getFile(request: ServerRequest): Mono<ServerResponse> {
    val parsedJson =
        objectMapper.readValue(readFile("fileName.json"), JsonModel::class.java)

    // modify parsed json

    return ok().contentType(APPLICATION_JSON).bodyValue(parsedJson)
}

private fun readFile(fileName: String) =
    DefaultResourceLoader()
        .getResource(fileName)
        .inputStream.bufferedReader().use { it.readText() }

我注意到 JsonObjectDecoder Netty 中的类,但我不知道是否可以应用于我的用例。

那么读取/解析资源文件的 react 方式是什么?

最佳答案

在扩展@vins 答案后,我得出了以下解决方案:

Jackson2JsonDecoder()
    .decodeToMono(
        DataBufferUtils.read(
            DefaultResourceLoader()
                .getResource("$fileName.json"),
            DefaultDataBufferFactory(),
            4096
        ),
        ResolvableType.forClass(JsonModel::class.java), null, null
    )
    .map { it as JsonModel }

关于json - 使用 WebFlux 从资源中读取和解析文件的 react 方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61139186/

相关文章:

带有 json 的 php web 服务

kotlin - 在Kotlin中从另一个收藏夹创建收藏夹

java - 在非 Activity 课上使用 Volley 和 Klaxon

constructor - 无法编译内部构造函数调用原因 :Primary constructor call expected

java - 如何覆盖任何 HttpHeader 以响应 WebClient?

javascript - AJAX 返回错误 JSON 输入意外结束

c# - 未知的网络方法,尝试使用 AJAX 发送 json

JavaScript访问本地json

java - Spring Webflux : Extract a value from a Mono and save it into another variable

java - 如何在 Spring boot FilterHandlerFunction 提交之前处理响应