Spring 5 Web Reactive-我们如何使用WebClient检索Flux中的流数据? [英] Spring 5 Web Reactive - How can we use WebClient to retrieve streamed data in a Flux?
本文介绍了Spring 5 Web Reactive-我们如何使用WebClient检索Flux中的流数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当前的里程碑(M4)文档显示了有关如何使用WebClient
检索Mono
的示例:
The current milestone (M4) documentation shows and example about how to retrieve a Mono
using WebClient
:
WebClient webClient = WebClient.create(new ReactorClientHttpConnector());
ClientRequest<Void> request = ClientRequest.GET("http://example.com/accounts/{id}", 1L)
.accept(MediaType.APPLICATION_JSON).build();
Mono<Account> account = this.webClient
.exchange(request)
.then(response -> response.body(toMono(Account.class)));
我们如何使用WebClient将流式数据(从返回text/event-stream
的服务中)获取到Flux中?它支持杰克逊自动转换吗?.
How can we get streamed data (from a service that returns text/event-stream
) into a Flux using WebClient? Does it support automatic Jackson conversion?.
这是我在上一个里程碑中所做的事情,但是API发生了变化,无法再找到如何做:
This is how I did it in a previous milestone, but the API has changed and can't find how to do it anymore:
final ClientRequest<Void> request = ClientRequest.GET(url)
.accept(MediaType.TEXT_EVENT_STREAM).build();
Flux<Alert> response = webClient.retrieveFlux(request, Alert.class)
推荐答案
这是使用新API可以实现相同目的的方法:
This is how you can achieve the same thing with the new API:
final ClientRequest request = ClientRequest.GET(url)
.accept(MediaType.TEXT_EVENT_STREAM).build();
Flux<Alert> alerts = webClient.exchange(request)
.retrieve().bodyToFlux(Alert.class);
这篇关于Spring 5 Web Reactive-我们如何使用WebClient检索Flux中的流数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文