liveBook · Manning

WebClient

As explained in Spring Start Here, Second Edition

WebClient is a non-blocking, reactive HTTP client API in Spring for making web requests and processing responses.

WebClient is Spring’s reactive HTTP client for calling REST endpoints. It is useful when an application makes non-blocking calls, such as a backend service requesting data from another service without keeping a thread waiting for the response. section 11.0 section 11.1

In a reactive application, work is organized into tasks and their dependencies. Rather than assigning one thread to wait through an entire request flow, available threads can work on other tasks while an I/O operation is pending. Independent tasks can also run in parallel. section 11.4

Calling a payment service

A payment proxy can use a WebClient bean built with WebClient.builder().build(). To create a payment, it starts a POST request, supplies the /payment URI and requestId header, and provides the payment as a Mono. The retrieve() and bodyToMono(Payment.class) calls describe how to obtain the response as a Mono<Payment>. section 11.4

Listing 11.9 Implementing a proxy class with WebClient
@Component
public class PaymentsProxy {
 
  private final WebClient webClient;
 
  @Value("${name.service.url}")    #A
  private String url;
 
  public PaymentsProxy(WebClient webClient) {
    this.webClient = webClient;
  }
 
  public Mono<Payment> createPayment(
    String requestId, 
    Payment payment) {
    return webClient.post()     #B
              .uri(url + "/payment")    #C
              .header("requestId", requestId)    #D
              .body(Mono.just(payment), Payment.class)   #E
              .retrieve()    #F
              .bodyToMono(Payment.class); #G
  }
}

Follow the proxy’s request chain from POST through response conversion. section 11.4

A Mono represents the producer used in this example. The proxy returns it instead of waiting for a Payment. In the reactive flow, WebFlux subscribes to the returned producer; WebClient also subscribes to the task supplying the request body. section 11.4

Figure 11.10 The tasks chain in a reactive app. When building a reactive web app, we define the tasks and the dependencies between them. The WebFlux functionality, initiating the HTTP request, subscribes to the task we create through the producer the controller’s action returns. In our case, this producer is the one we get by sending the HTTP request with WebClient. For WebClient to make the request, it subscribes to another task that provides the request body.

The task-chain diagram shows how the response and request-body producers connect. section 11.4

Choose WebClient for an application designed around a reactive approach. For a standard, nonreactive application, RestClient or OpenFeign is a simpler fit; using WebClient merely to make blocking calls adds reactive machinery without adopting the approach. section 11.1 section 11.6

How Spring Start Here, Second Edition covers it

How deep it goes
Explains reactive task dependencies and a request chain, while deferring a full treatment of reactive applications.
Examples
Uses a banking debt calculation and a payment-service POST proxy to illustrate reactive work and a WebClient request.
What it assumes
Recommends learning application basics before studying the advanced reactive approach.
What you can do after
Apply it via WebClient to configure a client and construct a reactive payment request and response chain.
How it connects
Connects WebClient with reactive programming, non-blocking I/O, Mono, WebFlux, RestClient, and OpenFeign.

Also explained in

Compare how 2 books explain WebClient →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring Start Here, Second Edition. The section links point to the parts of the book it draws on.