Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring WebClient as an alternative to RestTemplate

The current javadoc of RestTemplate states:

NOTE: As of 5.0, the non-blocking, reactive org.springframework.web.reactive.client.WebClient offers a modern alternative to the RestTemplate with efficient support for both sync and async, as well as streaming scenarios. The RestTemplate will be deprecated in a future version and will not have major new features added going forward.

We are writing a new project using spring boot 2.0.6 and spring 5.0.10.

Seeing that restTemplate is going to be deprecated we decided to use the new WebClient which should have support for synch calls as well. But I couldn't find any documentation on how that should be achieved.

I have used block for this as in the code below:

ResponseEntity<String> response = webClient.get()
            .uri(url)
            .exchange()
            .flatMap(r -> r.toEntity(String.class))
            .block();

However this is throwing the exception below when called from a spring controller

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

So how exactly should WebClient be used in a synchronous way?

EDIT: My pom.xml looks like this:

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
like image 706
Ramona Cristea Avatar asked Oct 31 '18 12:10

Ramona Cristea


People also ask

Which one is better WebClient or RestTemplate?

The notification will be produced only when the response is ready. RestTemplate will still be used. But in some cases, the non-blocking approach uses much fewer system resources compared to the blocking one. So, WebClient is a preferable choice in those cases.

What is the alternative of RestTemplate in Spring boot?

If you have Spring WebFlux on your classpath, you can also choose to use WebClient to call remote REST services. Compared to RestTemplate , this client has a more functional feel and is fully reactive. You can create your own client instance with the builder, WebClient.

Is RestTemplate going to be deprecated?

WebClient offers support for both synchronous and asynchronous HTTP requests and streaming scenarios. Therefore, RestTemplate will be marked as deprecated in a future version of the Spring Framework and will not contain any new functionalities. RestTemplate is based on a thread-per-request model.

Why do we use WebClient?

WebClient is meant to be used in a reactive environment, where nothing is tied to a particular thread. Here we see how to create a WebClient instance, and use it to build and execute an HTTP request. Spring WebClient is a non-blocking, reactive client to perform HTTP requests, a part of Spring Webflux framework.


1 Answers

If your application is just using spring-boot-starter-webflux, it means both the server and client will be using Spring WebFlux. In this case, it is forbidden to call a block operator within a Controller handler, as it will block one of the few server threads and will create important runtime issues.

If the main driver behind this is to use WebClient, then you can depend on both spring-boot-starter-web and spring-boot-starter-webflux. Your Spring Boot application will still use Spring MVC on the server side and you'll be able to use WebClient as a client. In that case, you can call block operators or even use Flux or Mono as return types in your controllers, as Spring MVC supports that. You can even gradually introduce WebClient in an existing Spring MVC application.

like image 114
Brian Clozel Avatar answered Sep 28 '22 01:09

Brian Clozel