RestTemplate is Spring's classic synchronous, blocking HTTP client. It wraps the low-level HTTP plumbing so you can call a REST API with one method call, and it converts request and response bodies to and from Java objects through HttpMessageConverter instances. It is still widely used, but it is in maintenance mode: since Spring Framework 6.1 the recommended client for new blocking code is RestClient.
This article explains what RestTemplate does, how to use it in Spring Boot, how to handle errors and timeouts, and when to move to RestClient or WebClient.
RestTemplate follows the Spring template pattern, like JdbcTemplate. You supply a URL, an HTTP method and optionally a body, and it takes care of:
HttpClient, Apache HttpClient, Jetty or OkHttp);Each call blocks the calling thread until the response arrives. That is simple to reason about and fine for most services, but it ties up one thread per in-flight request.
Spring Boot does not register a RestTemplate bean for you. It registers a RestTemplateBuilder, which applies Boot's message converters, and you build the instance yourself so you can set timeouts.
Always set both timeouts. Without them, a slow upstream service can hold your threads indefinitely. In Spring Boot 3.4 and later the timeout methods are named connectTimeout and readTimeout on the builder, and older 3.x releases used setConnectTimeout and setReadTimeout; check the Javadoc of the version you use.
The main methods map to HTTP verbs.
| Method | HTTP verb | Returns |
|---|---|---|
getForObject | GET | The body converted to your type |
getForEntity | GET | ResponseEntity with status, headers and body |
postForObject | POST | The body of the response |
postForEntity | POST | ResponseEntity |
put | PUT | Nothing |
delete | DELETE | Nothing |
exchange | Any | ResponseEntity, full control over headers and generic types |
A small client that uses them:
Use URI template variables ({id}) instead of string concatenation. RestTemplate encodes them for you. Use ParameterizedTypeReference whenever the response is generic, such as List<User>, because Java erases generic types at runtime.
By default RestTemplate throws a subclass of RestClientException for error statuses: HttpClientErrorException for 4xx and HttpServerErrorException for 5xx. Network failures raise ResourceAccessException.
For a service-wide policy, register a custom ResponseErrorHandler through builder.errorHandler(...).
Pass headers through HttpEntity for a single call, or use a ClientHttpRequestInterceptor to apply them to every call, for example a bearer token or a correlation id.
Building the template from the injected RestTemplateBuilder also keeps Micrometer observation and tracing instrumentation working, because Boot customises the builder for you.
| Feature | RestTemplate | RestClient | WebClient |
|---|---|---|---|
| Programming model | Blocking | Blocking | Non-blocking, reactive |
| API style | Many overloaded methods | Fluent | Fluent |
| Available since | Spring 3 | Spring 6.1 | Spring 5 |
| Status | Maintenance mode | Recommended for blocking code | Recommended for reactive code |
RestClient offers the same blocking behaviour with a fluent API and reuses the same message converters and request factories:
For new code, choose RestClient. For declarative clients, Spring's @HttpExchange interfaces work on top of RestClient, WebClient or RestTemplate. Existing RestTemplate code keeps working, and you can migrate it gradually. The Spring team has announced plans to deprecate RestTemplate in a future release, so check the release notes of your Spring version before starting a large new project on it.
It is not deprecated in every release, but it is in maintenance mode and receives only bug fixes. The Spring team recommends RestClient for blocking calls and WebClient for reactive calls, and has announced a future deprecation.
Yes. Once configured, a single RestTemplate instance can be shared across threads, so define it as a singleton bean.
Use RestTemplateBuilder with connectTimeout and readTimeout, or configure the underlying ClientHttpRequestFactory directly. Boot also supports client settings through ClientHttpRequestFactorySettings in recent versions.
No. AsyncRestTemplate was removed in Spring 6. Use WebClient, or run blocking calls on virtual threads, which scale much better than platform threads for this workload.
RestTemplate is a synchronous HTTP client that turns REST calls into simple method calls with automatic JSON conversion and exception mapping. Build it from RestTemplateBuilder, always set timeouts, use URI variables and ParameterizedTypeReference, and handle RestClientException subclasses. Keep it for existing code, and prefer RestClient or WebClient for new work.