我在Spring Controller中有一个Flux of DataBuffer,我想将它作为流附加到StreamingResponseBody。我已经阅读了许多类似方法的答案,但没有一个与此完全匹配。我不想将整个文件加载到内存中。我希望它流。
@GetMapping(value="/attachment")
public ResponseEntity<StreamingResponseBody> get(@PathVariable long attachmentId) {
Flux<DataBuffer> dataBuffer = this.myService.getFile(attachmentId);
StreamingResponseBody stream = out -> {
// what do do here?
}
return ResponseEntity.ok()
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.body(stream);
}
}
字符串
编辑:myService.getFile()
public Flux<DataBuffer> gteFile(long attachmentId) {
return this.webClient.get()
.uri("https://{host}/attachments/{attachmentId}", host, attachmentId)
.attributes(clientRegistrationId("attachmentClient"))
.accept(MediaType.ALL)
.exchangeToFlux(clientResponse -> clientResponse.bodyToFlux(DataBuffer.class));
}
型
1条答案
按热度按时间wvt8vs2t1#
如果你使用WebFlux,不需要返回
StreamingResponseBody
,你可以直接返回Flux<DataBuffer>
,这样它就可以流传输,也可以不阻塞。如果你想添加一些标题/自定义你的响应,你可以返回
Mono<ResponseEntity<Flux<DataBuffer>>>
:字符串
如果你想使用
StreamingResponseBody
(这意味着你在web层上使用阻塞的Spring MVC),那么你可以这样做:型
然后从控制器返回