Great observation — the usage of WebClient
, Flux
, and Mono
in Spring WebFlux follows a combination of modern design patterns and reactive programming paradigms. Let’s break it down:
WebClient
Instead of passing all values to a constructor or method, you build the request step by step:
WebClient.create()
.get()
.uri("/example")
.header("Authorization", "Bearer token")
.retrieve()
.bodyToMono(String.class);
This is Builder Pattern:
.get().uri().retrieve()
) is an example of Fluent Interface.Flux
and Mono
Mono
and Flux
is asynchronous, non-blocking event handling, which is the essence of the Reactor Pattern.Mono<String> result = webClient.get().uri("/data").retrieve().bodyToMono(String.class);
result.subscribe(System.out::println);
This is a Publisher-Subscriber or Observer pattern:
Mono
/Flux
are Publishers.subscribe()
to them to receive data when it’s ready.Using method chaining instead of one giant method:
Component | Pattern(s) Used | Purpose |
---|---|---|
WebClient |
Builder, Fluent Interface | Build HTTP requests fluently |
Mono/Flux |
Reactor, Publisher-Subscriber | Non-blocking async programming |
WebClient call chain | Chain of Responsibility, Builder | Step-by-step configuration |
Let me know if you’d like a visual UML or example transforming this into a custom builder class.