来自带有 Spring/Project 反应器的 HTTP 客户端的流响应 [英] Stream response from HTTP client with Spring/Project reactor

查看:46
本文介绍了来自带有 Spring/Project 反应器的 HTTP 客户端的流响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将响应从响应式 HTTP 客户端流式传输到控制器,而无需在应用程序内存中随时保留整个响应主体?

How to stream response from reactive HTTP client to the controller without having the whole response body in the application memory at any time?

几乎所有项目反应器客户端的示例都返回 Mono.据我了解,反应式流是关于流式传输的,而不是全部加载然后发送响应.

Practically all examples of project reactor client return Mono<T>. As far as I understand reactive streams are about streaming, not loading it all and then sending the response.

是否有可能返回一种Flux,从而可以将大文件从某些外部服务传输到应用程序客户端,而无需使用大量 RAM 内存来存储中间结果?

Is it possible to return kind of Flux<Byte> to make it possible to transfer big files from some external service to the application client without a need of using a huge amount of RAM memory to store intermediate result?

推荐答案

我不认为在你的场景中你需要创建一个事件流,因为事件流更多地用于实时发出事件我认为你最好这样做这个.

I dont think that in your scenario you need to create an event stream because event stream is more used to emit event in real time i think you better do it like this.

@GetMapping(value = "bytes")
public Flux<Byte> getBytes(){
    return byteService.getBytes();
}

并且您可以将其发送为流.如果你仍然想要它作为一个流

and you can send it es a stream. if you still want it as a stream

@GetMapping(value = "bytes",produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<List<Byte>> getBytes(){
    return byteService.getBytes();
}

这篇关于来自带有 Spring/Project 反应器的 HTTP 客户端的流响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆