当前里程碑(M4)文档展示了如何使用WebClient
检索Mono
的示例。
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)));
如何使用text/event-stream
将流数据(来自返回WebClient的服务)传输到流中?它支持自动杰克逊转换吗?
在上一个里程碑中,我就是这样做的,但是API已经改变了,再也找不到该怎么做了:
final ClientRequest<Void> request = ClientRequest.GET(url)
.accept(MediaType.TEXT_EVENT_STREAM).build();
Flux<Alert> response = webClient.retrieveFlux(request, Alert.class)
发布于 2017-01-07 16:34:51
这就是如何使用新的API实现同样的目标:
final ClientRequest request = ClientRequest.GET(url)
.accept(MediaType.TEXT_EVENT_STREAM).build();
Flux<Alert> alerts = webClient.exchange(request)
.retrieve().bodyToFlux(Alert.class);
发布于 2017-08-04 09:16:32
在Spring5.0中,是这样做的:
public Flux<Alert> getAccountAlerts(int accountId){
String url = serviceBaseUrl+"/accounts/{accountId}/alerts";
Flux<Alert> alerts = webClient.get()
.uri(url, accountId)
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.bodyToFlux( Alert.class )
.log();
return alerts;
}
https://stackoverflow.com/questions/41396430
复制相似问题