Sub topic 2
RestClient, RestTemplate and WebClient: which to use
This chapter assumes you know Java 17 (records, lambdas), basic Spring Boot (beans, dependency injection), and HTTP methods and status codes. Spring offers three HTTP clients for calling remote services. Choosing between them depends on how your application handles threads and how old your code is.
The three clients at a glance
- RestTemplate is the original synchronous client, with template-style methods such as
getForObjectandpostForEntity. It is in maintenance mode. It still works and receives fixes, but it gets no new features. - WebClient is the reactive client from Spring WebFlux. It returns
MonoandFluxtypes and is non-blocking. - RestClient was added in Spring Framework 6.1 (Spring Boot 3.2 and later). It is synchronous like RestTemplate, but it has a fluent API like WebClient.
All three can send the same HTTP requests. The difference is in the programming model.
Comparing them
| Aspect | RestTemplate | RestClient | WebClient |
|---|---|---|---|
| Blocking model | Blocking | Blocking | Non-blocking |
| API style | Many overloaded methods | Fluent builder | Fluent builder |
| Return types | Plain objects, `ResponseEntity` | Plain objects, `ResponseEntity` | `Mono`, `Flux` |
| Extra dependency | None beyond `spring-web` | None beyond `spring-web` | `spring-webflux` |
| Status | Maintenance mode | Recommended for blocking code | Recommended for reactive code |
RestClient and RestTemplate share the same infrastructure: message converters, request factories and interceptors. Because of that, you can often move from one to the other without changing how JSON is read or how timeouts are set.
Comparing the same call
Here is one GET request written with each client. All three assume a Todo record and a base URL of https://api.example.com.
import org.springframework.web.client.RestClient;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.client.WebClient;
record Todo(long id, String title, boolean completed) {}
class ClientComparison {
Todo withRestTemplate(RestTemplate restTemplate, long id) {
return restTemplate.getForObject(
"https://api.example.com/todos/{id}", Todo.class, id);
}
Todo withRestClient(RestClient restClient, long id) {
return restClient.get()
.uri("/todos/{id}", id)
.retrieve()
.body(Todo.class);
}
Todo withWebClient(WebClient webClient, long id) {
return webClient.get()
.uri("/todos/{id}", id)
.retrieve()
.bodyToMono(Todo.class)
.block();
}
}The RestClient version returns a Todo directly, and the calling thread waits until the response arrives. The WebClient version returns a Mono<Todo>. Calling block() turns it back into a plain value, which is what you would do only in a blocking application. If you find yourself calling block() on every WebClient call, RestClient is the better fit.
How to decide
Use these questions in order:
- Is the rest of your application reactive? If your controllers return
MonoorFluxand you use a reactive stack, use WebClient. Blocking inside a reactive pipeline can stall the event loop, which is the small set of threads that serves all requests. - Do you need streaming or parallel composition? WebClient is built for streaming responses and combining many calls without a thread per call. RestClient is not.
- Is this new code in a Spring MVC application? Use RestClient. It gives you the readable fluent style without adding reactive types or the WebFlux dependency.
- Is this existing code that already uses RestTemplate? Leave it unless you have a reason to change. There is no deadline for migrating, but new code should prefer RestClient.
Common mistakes
- Adding WebFlux only to get a fluent API. RestClient already gives you that. Adding
spring-webfluxfor a blocking application brings in a second programming model for no gain. - Calling
block()inside reactive code. This can throw an exception or starve the event loop. Stay with WebClient end to end in reactive applications. - Assuming RestTemplate is removed. It is not deprecated for removal. It is simply not where new features go.
- Expecting different error behavior. RestClient and RestTemplate both throw exceptions for 4xx and 5xx responses by default. Chapter-level details on customizing this come later.
Exercises
Exercise
Your Spring MVC service makes five sequential calls to other services and returns plain objects from its controllers. Which client should new code use, and why?
Show answer
RestClient. The application is blocking and not reactive, so there is no benefit from WebClient's non-blocking model. RestClient offers the fluent API without reactive types or the WebFlux dependency, and it is the recommended client for new blocking code.
Exercise
A teammate wants to add WebClient to a blocking application and call .block() on every request because "the API is nicer." What would you suggest instead?
Show answer
Use RestClient. It has a similar fluent API, returns plain values, and needs no spring-webflux dependency. Calling block() everywhere adds reactive types that are immediately discarded.