RestTemplate Is Deprecated for Removal: Moving to RestClient
Spring Boot 4.2, released on 19 November 2026, deprecates its RestTemplate and TestRestTemplate support because Spring Framework 7.1 has deprecated RestTemplate for removal. Most Spring services still use it somewhere. A practical migration guide: what changed, the one-line bridge that lets you migrate gradually, how to translate the common patterns, and how to move tests to RestTestClient.
- #spring-boot
- #java
- #migration
- #backend
- #rest
RestTemplate has been "in maintenance mode" since Spring 5, which is the software equivalent of being moved to a desk near the exit. For years that meant nothing in practice. It worked, it was everywhere, and nobody was in a hurry.
That changed this month. Spring Boot 4.2, released on 19 November, deprecates its RestTemplate and TestRestTemplate support in favour of RestClient and RestTestClient. The release notes give the reason: Spring Framework 7.1 has deprecated RestTemplate for removal.
"Deprecated" you can ignore for a while. "Deprecated for removal" means there's a release on the calendar where your code stops compiling. Better to move on your own schedule than on the framework's.
The good news: RestClient was designed as the replacement, the concepts map almost one to one, and you can migrate one call at a time.
What exactly is deprecated
In Spring Boot 4.2, the supporting infrastructure is deprecated, including:
RestTemplateBuilderand the auto-configuration that provides itRestTemplateCustomizer(the equivalent isRestClientCustomizer)TestRestTemplatefor integration tests (replaced byRestTestClient)- The test support that auto-configures
MockRestServiceServerforRestTemplate
Your code still compiles and runs today. You'll just see deprecation warnings, which is the framework clearing its throat politely.
Why RestClient is better anyway
Even without the deprecation, there are good reasons to switch:
- A fluent API that reads top to bottom, instead of a long list of
exchangeoverloads with positional arguments. - Clear error handling per call or per client, instead of one global
ResponseErrorHandler. - The same style as
WebClient, without needing reactive types. If your team knows one, they can read the other. - First-class support for newer features, such as HTTP interface clients and API version headers.
It's synchronous, uses the same message converters, and runs on the same HTTP libraries. Nothing about your threading model changes.
The bridge: migrate gradually
You don't need a big-bang rewrite. RestClient can be created from an existing RestTemplate, inheriting its configuration:
RestClient restClient = RestClient.create(restTemplate);Or start from a builder if you want to add to it:
RestClient restClient = RestClient.builder(restTemplate)
.defaultHeader("X-Service", "orders")
.build();The new client reuses the template's request factory, interceptors, message converters and URI template handling. So you can keep one configured RestTemplate bean while you migrate call sites one by one, then delete it when the last call is gone. Low risk, easy to review, and no "migration week".
Translating the common patterns
Simple GET
// Before
Order order = restTemplate.getForObject("/orders/{id}", Order.class, id);
// After
Order order = restClient.get()
.uri("/orders/{id}", id)
.retrieve()
.body(Order.class);GET with the full response
// Before
ResponseEntity<Order> response =
restTemplate.getForEntity("/orders/{id}", Order.class, id);
// After
ResponseEntity<Order> response = restClient.get()
.uri("/orders/{id}", id)
.retrieve()
.toEntity(Order.class);POST with a body
// Before
Order created = restTemplate.postForObject("/orders", request, Order.class);
// After
Order created = restClient.post()
.uri("/orders")
.contentType(MediaType.APPLICATION_JSON)
.body(request)
.retrieve()
.body(Order.class);Headers and generic types
The exchange plus HttpEntity plus ParameterizedTypeReference combination is where RestTemplate code gets hardest to read:
// Before
HttpHeaders headers = new HttpHeaders();
headers.setBearerAuth(token);
ResponseEntity<List<Order>> response = restTemplate.exchange(
"/orders?status={status}", HttpMethod.GET, new HttpEntity<>(headers),
new ParameterizedTypeReference<List<Order>>() {}, status);
// After
List<Order> orders = restClient.get()
.uri("/orders?status={status}", status)
.headers(h -> h.setBearerAuth(token))
.retrieve()
.body(new ParameterizedTypeReference<>() {});Same request, half the ceremony, and you can read it without counting arguments.
Error handling
With RestTemplate, a 4xx or 5xx throws an exception, and custom behaviour needs a ResponseErrorHandler on the whole template. RestClient lets you decide per call:
Order order = restClient.get()
.uri("/orders/{id}", id)
.retrieve()
.onStatus(status -> status.value() == 404, (req, res) -> {
throw new OrderNotFoundException(id);
})
.body(Order.class);Or set it once on the builder with defaultStatusHandler(...) for client-wide rules. That's handy for mapping a downstream service's errors into your own exception types in one place.
Interceptors and customizers
Interceptors work the same way, registered on the builder:
RestClient client = restClientBuilder
.requestInterceptor(new CorrelationIdInterceptor())
.build();If you have RestTemplateCustomizer beans (for example, adding auth or tracing to every client), rewrite them as RestClientCustomizer beans. Spring Boot applies them to the auto-configured RestClient.Builder, the same way it did for templates.
One habit to keep: inject RestClient.Builder rather than calling RestClient.builder() yourself. The injected builder carries Boot's configuration, including observability, so your HTTP calls keep showing up in traces.
Moving the tests
Integration tests: TestRestTemplate to RestTestClient
RestTestClient brings the WebTestClient style of assertions to blocking tests:
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureRestTestClient
class OrderApiIT {
@Autowired
RestTestClient client;
@Test
void returnsOrder() {
client.get().uri("/orders/{id}", 42)
.exchange()
.expectStatus().isOk()
.expectBody(Order.class)
.value(order -> assertThat(order.id()).isEqualTo(42));
}
}It reads better than TestRestTemplate plus a separate block of assertions, and the failure messages are clearer.
Unit tests with MockRestServiceServer
MockRestServiceServer works with RestClient too. Bind it to the builder before building the client:
RestClient.Builder builder = RestClient.builder();
MockRestServiceServer server = MockRestServiceServer.bindTo(builder).build();
OrderClient orderClient = new OrderClient(builder.build());
server.expect(requestTo("/orders/42"))
.andRespond(withSuccess(orderJson, MediaType.APPLICATION_JSON));Existing expectations and response stubs usually carry over unchanged.
Consider skipping a step: HTTP interface clients
If you're rewriting a client anyway, look at HTTP interface clients. You declare an interface and Spring generates the implementation on top of RestClient:
@HttpExchange("/orders")
interface OrderApi {
@GetExchange("/{id}")
Order get(@PathVariable String id);
@PostExchange
Order create(@RequestBody CreateOrderRequest request);
}For services that call the same downstream API from many places, this removes a whole layer of hand-written client code. It isn't required for the migration, but it's a good moment to consider it.
A migration plan that fits in normal sprints
- Upgrade to Spring Boot 4.2 and list the deprecation warnings. That's your inventory.
- Add the bridge: create a
RestClientfrom each configuredRestTemplate. - Migrate call sites service by service, starting with the simplest. Each one is a small, reviewable change.
- Rewrite customizers as
RestClientCustomizerbeans. - Move tests to
RestTestClientand bindMockRestServiceServerto the new builders. - Delete the
RestTemplatebeans once nothing uses them, and add a build check (for example, an ArchUnit rule) so nobody adds one back.
Step 6 sounds fussy, but without it someone will paste a RestTemplate example from a 2017 Stack Overflow answer within a month. It happens to the best codebases.
The takeaway
Spring Boot 4.2, released on 19 November 2026, deprecates its RestTemplate and TestRestTemplate support because Spring Framework 7.1 has deprecated RestTemplate for removal. Nothing breaks today. But "for removal" turns a polite warning into a countdown, and the only choice left is whether you migrate on your schedule or the framework's.
The migration is smaller than it looks. RestClient.create(restTemplate) gives you a new client on top of the configuration you already have, so migrated and unmigrated call sites behave the same while you work through them one small diff at a time. The common calls translate almost line for line, and error handling gets better on the way, because each call can decide what a 404 means. Swap RestTemplateCustomizer for RestClientCustomizer, move integration tests from TestRestTemplate to RestTestClient, and inject RestClient.Builder instead of building one yourself, so your HTTP calls keep showing up in traces.
When the last call site is gone, delete the bean and add a build rule so a 2017 Stack Overflow answer can't bring it back. RestTemplate had a long, useful career, and it's earned its retirement. Give it a proper send-off before the framework does it for you.
/share

Kishore K Sharma
Module Lead | Senior Full Stack Engineer | Java · Spring Boot · Distributed Systems · AWS | Building Scalable Cloud-Native Platforms
Available for contract work and remote full-time roles. See what I take on.
/seen in production