Video in progress
RestTemplate, RestClient or WebClient — which one would you pick for a new project?
For a regular blocking app — RestClient (Spring 6.1+), with a fluent API. If it's reactive or you make lots of parallel calls — WebClient. RestTemplate still works, but it's in "maintenance" mode; don't use it in new code.
📖 Answer
@Bean
RestClient paymentClient(RestClient.Builder builder) {
return builder
.baseUrl("https://api.pay.uz")
.defaultHeader("Authorization", "Bearer " + token)
.build();
}
PaymentResponse r = client.post()
.uri("/v1/payments")
.body(new PaymentRequest(orderId, amount))
.retrieve()
.onStatus(HttpStatusCode::is4xxClientError, (req, res) -> {
throw new PaymentRejected(res.getStatusCode());
})
.body(PaymentResponse.class);
Comparison:
| RestTemplate | RestClient | WebClient | |
|---|---|---|---|
| Style | old, lots of overloads | fluent | fluent, reactive |
| Blocking | yes | yes | no (block() is possible) |
| Required library | web | web | webflux |
With Java 21 virtual threads, a blocking RestClient easily handles lots of parallel requests too — you don't have to move to WebFlux just for parallelism.
Whichever you pick, set timeouts. By default they may be unlimited, and one slow external service can tie up every thread in your app.
Sign up to mark studied questions and save your progress.