Search code examples
javareactive-programmingspring-webfluxspring-webclient

How to return just a String using Webflux - webclient?


I would like to make this call and return a string but I'm having this error:

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

I dont understand why am I doing wrong? WebClient supports asynchronous as well as synchronous calls.

This a springboot project with just 3 dependencies.

How can I return just a string like a normal synchronous call?

@RestController
public class HomeController {

@GetMapping("/")
public String home() {
String resp = webclient.get()                             
                       .uri(ANY_URL).retrieve()
                       .bodyToMono(String.class)
                       .block();
return resp;

 }


}

pom.xml
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

Thanks.


Solution

  • Fixed by adding spring-boot-starter-web

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>