2

目前的里程碑(M4)的文件給出了一個示例如何使用WebClient檢索MonoSpring 5 Web Reactive - 我們如何使用WebClient在Flux中檢索流數據?

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服務)爲流量使用Web客戶端?它支持自動傑克遜轉換嗎?

這是我在以前的里程碑做到了,但是API已經改變,無法找到如何做到這一點了:

final ClientRequest<Void> request = ClientRequest.GET(url) 
    .accept(MediaType.TEXT_EVENT_STREAM).build(); 
Flux<Alert> response = webClient.retrieveFlux(request, Alert.class) 
+0

目前的里程碑是M4 ...所以你可能要再次檢查文檔,M4版本中已完成很多工作來完成反應性功能。 –

+0

我已經檢查過它,甚至目前的快照,但沒有關於這方面的細節。 – codependent

+0

我查了Spring Framework 5 RC3,看來ClientRequest現在還沒有GET方法 – Sergey

回答

3

這是如何可以實現同樣的事情與新API:

final ClientRequest request = ClientRequest.GET(url) 
     .accept(MediaType.TEXT_EVENT_STREAM).build(); 
Flux<Alert> alerts = webClient.exchange(request) 
     .flatMap(response -> response.bodyToFlux(Alert.class)); 
2

使用Spring 5.0.0.RELEASE你這是怎麼做到這一點:

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; 
} 
相關問題