3

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)));

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?.

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)
Brian Clozel
  • 56,583
  • 15
  • 167
  • 176
codependent
  • 23,193
  • 31
  • 166
  • 308

2 Answers2

7

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);
Brian Clozel
  • 56,583
  • 15
  • 167
  • 176
7

With Spring 5.0.0.RELEASE this is how you do it:

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;
}
codependent
  • 23,193
  • 31
  • 166
  • 308