In Spring 5, Spring introduced a component in the new Web Reactive framework that helps to build reactive and non-blocking web applications. A common requirement in web applications is to make HTTP calls to other services. Before Spring 5, we were using RestTemplate as the primary technology for client-side HTTP access. Which was simple and always blocking web client, which is now in maintenance mode.

Overview of WebClient
In the Spring framework, the WebClient is a reactive, non-blocking client for HTTP requests. It is part of the Spring WebFlux module, which provides support for reactive programming in Spring. The WebClient provides a functional and flexible API for making HTTP requests, and it can be used for tasks such as consuming REST APIs, uploading data, and performing other web-related operations. It provides an API for making HTTP requests and processing the response asynchronously, without blocking the calling thread. This makes it suitable for developing reactive applications that require high performance and low latency.
WebClient supports common HTTP methods such as GET, POST, PUT, DELETE, and others. And can be used to call RESTful APIs or any other web services. It also provides features such as request and response manipulation, request chaining, and error handling. The solution offers support for both synchronous and asynchronous operations, making it suitable also for applications running on a Servlet Stack.
Let’s look at an example that demonstrate the working of Webclient.
Dependency
In order to use WebClient in our Spring Boot project, we need to add a dependency on the WebFlux library. Like any other Spring Boot dependency, we have to add a starter dependency, as spring-boot-starter-webflux module into the project.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
Steps to work with Webclient
- Create an instance
- make a request
- handle the response
Create an instance of Webclient
There are three options to create webclient instance:
Method-1: Let’s start with first one creating a WebClient object with default settings:
WebClient webClient = WebClient.create();
Method-2: Alternatively, there is a more flexible way to create a web client instance by specifying the base URL of the service.
WebClient webClient = WebClient .create("https://gorest.co.in/public/v2");
Method-3: The most flexible way of creating a WebClient instance is to use its own builder (WebClient.Builder) using DefaultWebClientBuilder class. It allows full customization on the client behavior as per our requirement.
WebClient webClient = WebClient.builder().baseUrl("https://gorest.co.in/public/v2") .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).build();
Sending HTTP Requests and Handling API Response
public Flux<User> getUsers() {
return webClient.get() .uri("/users") .retrieve().bodyToFlux(User.class); }
We have specified http GET request method and URI to call. We called retrieve() method to get a Response for the request. To read the response body, we need to get Mono/Flux for the content of the response.
If we want to get only response body then using methods retrieve() and then bodyToFlux() and bodyToMono() methods. for example we are using bodyToFlux(User.class) in our example.