Home > Mobile >  Spring WebFlux - How to print response as String instead of object using WebClient
Spring WebFlux - How to print response as String instead of object using WebClient

Time:09-30

I have a Mono like below:

    private void getNodeDetail() {
        Mono<String> mono = webClient.get()
                .uri("/alfresco/api/-default-/public/alfresco/versions/1/nodes/f37b52a8-de40-414b-b64d-a958137e89e2")
                .retrieve().bodyToMono(String.class);

        System.out.println(mono.subscribe());
        System.out.println(mono.block());
    }

Questions: The first sysout shows me reactor.core.publisher.LambdaSubscriber@77114efe while using block() it shows me what I need (json String). But I want to use Aysnc approach. So, given above, does it mean my target system (Alfresco in this case) DO NOT support async calls? If that is not the case, how can I print the response on the console in String format using subscribe(), just like block() ?

CodePudding user response:

The subscribe() method returns a Disposable object:

 public final Disposable subscribe()

The expected way to print the response on the console is to actually use the doOnNext operator, like this:

private void getNodeDetail() {
    webClient.get()
            .uri("/alfresco/api/-default-/public/alfresco/versions/1/nodes/f37b52a8-de40-414b-b64d-a958137e89e2")
            .retrieve()
            .bodyToMono(String.class)
            .doOnNext(response -> System.out.println(response))
            .subscribe();
}
  • Related