RestTemplate is Spring's synchronous, blocking HTTP client for calling REST APIs. It wraps the low-level HTTP client, converts Java objects to and from JSON or XML through message converters, and gives you short methods such as getForObject() and postForEntity() for each HTTP verb.
Since Spring Framework 5 it has been in maintenance mode. For new code, Spring recommends RestClient (added in Spring Framework 6.1 and Spring Boot 3.2) with a very similar fluent API, or WebClient when you need non-blocking calls. RestTemplate is still widely used, so you will meet it in many codebases.
In Spring Boot, inject RestTemplateBuilder rather than calling new RestTemplate(). The builder applies Boot's auto-configured message converters and lets you set timeouts.
Always set timeouts. A template without them can block a thread indefinitely if the remote service stalls.
getForObject returns only the body, while getForEntity and postForEntity return a ResponseEntity with status code and headers. URI variables such as {id} are expanded and encoded for you, which is safer than concatenating strings.
Use exchange() when you need custom headers, a request body with a non-default method, or a generic response type.
ParameterizedTypeReference keeps the generic type information that would otherwise be erased, so Jackson can build a List<User> instead of a list of maps.
By default, 4xx and 5xx responses throw HttpClientErrorException and HttpServerErrorException. Network failures and timeouts throw ResourceAccessException. You can change this with a custom ResponseErrorHandler.
| Feature | RestTemplate | RestClient | WebClient |
|---|---|---|---|
| Style | Template methods | Fluent API | Fluent, reactive |
| Blocking | Yes | Yes | No (can block) |
| Available since | Spring 3 | Spring 6.1 | Spring 5 |
| Status | Maintenance mode | Recommended for sync | Recommended for reactive |
The same call with RestClient:
You can also build a RestClient from an existing RestTemplate with RestClient.create(restTemplate), which makes migration gradual.
RestTemplate bean and reuse it; it is thread-safe once configured.It is not formally deprecated, but it is in maintenance mode and receives only minor changes. New projects should choose RestClient or WebClient.
Yes, once configured. Share a single instance instead of creating one per request.
Wrap them in HttpHeaders, put them in an HttpEntity, and call exchange(). Or register a ClientHttpRequestInterceptor to add headers to every request.
Use exchange() with a ParameterizedTypeReference<List<T>>, or getForObject(url, T[].class) and convert the array.
RestTemplate simplifies consuming REST APIs in Spring by handling URI expansion, serialization and error mapping. Build it with RestTemplateBuilder, set timeouts, and use exchange() for full control. For new applications, prefer RestClient, which keeps the blocking model with a more modern API.