这个问题类似于Spring reactive streaming data from regular WebClient request不同的是我没有立即从我的 WebClient 获取 JSON 数组,而是这样的:
这个 JSON 对象可能非常大(~100MB),因此需要处理并流式传输到客户端,而不是解析。这是我似乎能够获得正确语义的唯一方法:
{
"result-set":{
"docs":[
{
"id":"auhcsasb1005_100000"
},
{
"id":"auhcsasb1005_1000000"
},
{
"id":"auhcsasb1005_1000001"
},
{
"id":"auhcsasb1005_1000002"
},
...
...
{
"EOF":true
}
]
}
}
WebClient.create()
.get()
.retrieve()
.bodyToMono(DontKnowWhatClass.class)
.flatMapMany(resultSet -> Flux.fromIterable(resultSet.getDocs()))
但这意味着我在内存中反序列化 100MB 或更多,然后从中创建通量。我想知道的是:我是否遗漏了一些重要的东西?我可以以某种方式从这样的对象创建 Flux 吗?遗憾的是,我现在有办法影响结果集对象的呈现方式。
最佳答案
您可以接受 ServerWebExchange
到您的 Controller ,该 Controller 有一个方法可以获取发布者 exchange.response.writeWith()
.
如果您有办法以块的形式解析有效负载,您只需创建一个发射部分的 Flux。
例如,如果您根本不关心有效载荷而只想按原样发送它:
@GetMapping("/api/foo/{myId}")
fun foo(exchange: ServerWebExchange, @PathVariable myId: Long): Mono<Void> {
val content: Flux<DataBuffer> = webClient
.get()
.uri("/api/up-stream/bar/$myId")
.exchange()
.flatMapMany { it.bodyToFlux<DataBuffer>() }
return exchange.response.writeWith(content)
}
确保检查内容协商设置,以避免出现意外缓冲。
关于spring-webflux - Spring WebClient : Parse + Stream very large JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54148326/