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)
4
  • Current milestone is M4... So you might want to check the documentation again, a lot of work has been done in the M4 release to complete the reactive features. Commented Dec 30, 2016 at 18:37
  • I have checked it and even the current snapshot but no details about this. Commented Dec 30, 2016 at 20:45
  • I checked Spring Framework 5 RC3 and it seems that ClientRequest doesn't have GET method now Commented Aug 3, 2017 at 18:15
  • @Sergey have a look at the new answer. Commented Aug 4, 2017 at 9:16

2 Answers 2

8

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);
Sign up to request clarification or add additional context in comments.

1 Comment

I think there is no retrieve() with exchange
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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.