The SpringWebFlux framework also provides a non-blocking, asynchronous HTTP client for making requests. WebClient offers APIs that can be configured with Java 8 lambdas, for processing data. At the backend, the WebClient API configures Netty to perform the asynchronous, non-blocking communication. Now, let's look at how we can use WebClient in our applications.
WebClient offers the following two methods for consuming data:
- Retrieve: This is the simplest method, which decodes the body into a Flux or Mono.
- Exchange: If we are interested in the response received, the exchange method is suited for this purpose. It provides the complete message, which can be converted back to a target type. Consider the following code for this: