我想知道从资源中读取、解析和提供文件的正确方法是什么。
目前,我做这样的事情:
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/