'How to get value out of mono

I'm trying to extract a String out out Mono.

Mono method:

public Mono<String> getVal() {
    return webClient.get()
        .uri("/service") 
        .retrieve()
        .bodyToMono(String.class);
}

Calling getVal():

String val = getVal().block();

I tried using block() but it returns the following error:

java.lang.IllegalStateException: block()/blockFirst()/blockLast() are blocking, 
which is not supported in thread reactor-http-nio-3

I suppose I would have to use a non-blocking method like subscribe() which I'm not to sure how to.



Solution 1:[1]

Ghokun has a better answer. But if anyone wants to use a blocking method:

I modified my getVal() method:

public String getVal() {
    String response = "";

        try {
            response = webClient.get()
                .uri("/service") 
                .retrieve()
                .bodyToMono(String.class)
                .toFuture()
                .get();
            
        } catch (Exception e) 
        {
            System.out.println(e.getMessage());
            
        }
                
        return response;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1