WebClient Basics

Reactive and Non-Blocking

WebClient is the reactive client from Spring WebFlux. It returns Mono (0 or 1 value) or Flux (many values) instead of waiting. Use it in WebFlux apps, or when you want to call several APIs in parallel.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
@Bean
WebClient ratesClient(WebClient.Builder builder) {
    return builder.baseUrl("https://api.rates.example").build();
}

public record Rate(String currency, BigDecimal inrValue) {}

Mono<Rate> usd = ratesClient.get().uri("/rates/{c}", "USD").retrieve().bodyToMono(Rate.class);
Mono<Rate> eur = ratesClient.get().uri("/rates/{c}", "EUR").retrieve().bodyToMono(Rate.class);

// both calls run at the same time; block() only in a classic (servlet) app
List<Rate> rates = Mono.zip(usd, eur, List::of).timeout(Duration.ofSeconds(3)).block();

RestClient or WebClient?

RestClientWebClient
StyleBlocking, simpleReactive (Mono/Flux)
Extra dependencyNo (spring-web)spring-boot-starter-webflux
Best inSpring MVC apps (most apps)WebFlux apps, streaming, many parallel calls
With virtual threads (Java 21)Scales very wellNot needed for scaling

Tip: In a normal Spring MVC app, start with RestClient. Reach for WebClient when you really need reactive streams.