Reactive HTTP Client With Spring 5 WebClient

This article is going to cover about Spring 5 WebClient, a non-blocking, reactive client for HTTP requests with Reactive Streams back pressure.

1. Introduction To Spring 5 WebClient

The WebClient is a non-blocking, reactive HTTP client which has been introduced in Spring 5 and is included in the spring-webflux module. Following are characteristics of the WebClient:

  • Provides a higher level API over HTTP client libraries. By default, it uses Reactor Netty
  • Returns Reactor Flux or Mono for output and accepts Reactive Streams Publisher as input (see Reactive Libraries).
  • Shares HTTP codecs and other infrastructure with the server functional
    web framework.

By comparison to the RestTemplate, the Spring 5 WebClient has some more advantages:

  • It offers a more functional
  • Enables fluent API that taking full advantage of Java 8 lambdas.
  • Supports both sync and async scenarios, including streaming, and brings the efficiency of non-blocking I/O.

2. Spring 5 WebClient API

2.1. Create A WebClient Object

Let’s see how we can create a WebClient object:

We have just configured a base URI for requests performed through the client to avoid repeating the same host, port, base path, or even query parameters with every request.

2.2. Exchange A Request With The exchange() Method

To exchange a request, we can use the exchange() method as the following example:

The method exchanges the request for a ClientResponse with full access to the response status and headers before extracting the body.

2.3. Exchange A Request With The retrieve() Method

Another way to exchange a request with the WebClient is to use the retrieve() method:

The retrieve() method is a variant of the exchange() method that provides the shortest path to retrieving the full response (i.e. status, headers, and body) where instead of returning Mono<ClientResponse> it exposes shortcut methods to extract the response body.

3. Spring 5 WebClient Examples

3.1. Preparation

Assume that we have a REST API to manage Book, produces and consumes JSON data with some basic API(s) as follows:

3.1.1. Read All Books

3.1.2. Read A Specific Book

3.1.3. Create a new book

3.1.4. Update a book

3.1.5. Delete a book

3.1.6. Source Code

The sample source code of the above REST API can be found on my Github project.

3.1.7. The Book POJO

We’re going to define a Book class which represent the response from the above REST API:

3.2. Maven Dependency

We’re going to use Spring Boot 2.0.0.M5 with spring-boot-starter-webflux. Let’s see all Maven dependencies needed in the following part of the pom.xml file:

3.3. Make An HTTP GET Request

In the following example, we’re going to make an HTTP GET request to the REST API to retrieve all the books:

And another example to retrieve a single book:

3.4. Make An HTTP POST Request (Create A Book)

To make an HTTP POST request, at first, we need to call the post() method of the WebClient in order to prepare an HTTP POST request. Then we optionally provide the body for the request and call the retrieve() or exchange() method:

3.4. Make An HTTP PUT Request (Update A Book)

To make an HTTP PUT request, at first, we need to call the put() method of the WebClient in order to prepare an HTTP PUT request. Then we optionally provide the body for the request and call the retrieve() method:

Notice that in above example, firstly we create a Book without the Author in the first request to the server. After that, we update the author for the book in the second request.

3.5. Make An HTTP DELETE Request (delete a book)

To make an HTTP DELETE request, at first, we need to call the delete() method of the WebClient in order to prepare an HTTP DELETE request. Then we optionally supply the data requested by the REST API for the request and call the retrieve() or exchange() method:

In the above example, we create a book first. Then we delete the book and validate the response.

3.6. Make An HTTP Request With Basic Authentication

To make an HTTP request with basic authentication, we can provide the WebClient a filter which adds an Authorization header for HTTP Basic Authentication, based on the given username and password. Notice that Spring 5 already provides us a basic authentication filter which can be found in the ExchangeFilterFunctions class.
Now, let’s see an example of using WebClient to make a request with basic authentication:

4. Conclusion

In this tutorial, we have just got through Spring 5 WebClient, a non-blocking, reactive client for HTTP requests. We’ve learned how to prepare for HTTP GET, POST, PUT, DELETE requests and exchange the request using the exchange() and retrieve() methods. We can see that the easiest way to exchange a request is to use the retrieve() method while we have more control over the response body when we use the exchange() method.

The sample code presented in this tutorial is available on my Github project. It’s a Maven based project, so it’s easy to be imported into IDEs such as IntelliJ, Eclipse, etc.

Below are other related articles:

Java REST Client Examples

Using Netflix Feign with Spring Cloud

Basic Authentication with Open Feign

File Uploading with Open Feign

Java REST Client Using Retrofit 2

Simple Java REST Client Using java.net.URL package

Java REST Client Using Spring RestTemplate

Java REST Client Using Apache Httpcomponents

Java REST Client Using Jersey Client

Java REST Client Using Resteasy Client

Java REST Client Using Resteasy Client Proxy Framework

Java REST Client Using Apache CXF Proxy-based API