# 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.

Author: Kishore K Sharma. Published: 2026-11-24. Canonical URL: https://kishorek.dev/writing/resttemplate-deprecated-move-to-restclient. Tags: spring-boot, java, migration, backend, rest.
License: © 2026 Kishore K Sharma. All rights reserved. Reproduction requires attribution and a link to https://kishorek.dev/writing/resttemplate-deprecated-move-to-restclient.

---
`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](https://spring.io/blog/2026/11/19/spring-boot-4-2-0-available-now/), released on 19 November, deprecates its `RestTemplate` and `TestRestTemplate` support in favour of `RestClient` and `RestTestClient`. The [release notes](https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-4.2-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:

- `RestTemplateBuilder` and the auto-configuration that provides it
- `RestTemplateCustomizer` (the equivalent is `RestClientCustomizer`)
- `TestRestTemplate` for integration tests (replaced by `RestTestClient`)
- The test support that auto-configures `MockRestServiceServer` for `RestTemplate`

Your code still compiles and runs today. You'll just see deprecation warnings, which is the framework clearing its throat politely.

![Top row: a box for Spring Framework 7.1 marked in danger colour as deprecating RestTemplate for removal, with an arrow to a box for Spring Boot 4.2, released 19 November 2026, marked as deprecating its supporting infrastructure. Below, four deprecated items on the left in dashed boxes each point to an accent-coloured replacement on the right: RestTemplate to RestClient, RestTemplateBuilder to an injected RestClient.Builder, RestTemplateCustomizer to RestClientCustomizer, and TestRestTemplate to RestTestClient with @AutoConfigureRestTestClient. A caption notes that everything still compiles today and each row is a swap you can make one file at a time.](/writing/restclient-migration-replacement-map.svg "Boot's deprecations follow from the Framework's, and every deprecated piece already has a named replacement.")

## 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 `exchange` overloads 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:

```java
RestClient restClient = RestClient.create(restTemplate);
```

Or start from a builder if you want to add to it:

```java
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".

![Three call sites on the left: OrderClient and InvoiceClient marked migrated, with accent arrows into a RestClient box built with create(restTemplate), and ShippingClient marked not yet, in a dashed box, with a plain arrow into the existing RestTemplate bean. Both the RestClient and the RestTemplate point to a single box on the right listing what is reused from the template: request factory, interceptors, message converters and URI templates. A caption says old and new call sites share one configuration so each migration is a small diff, and that once nothing points at RestTemplate you delete the bean.](/writing/restclient-migration-bridge.svg "The bridge means migrated and unmigrated code run on the same configuration, so nothing changes behaviour halfway through.")

## Translating the common patterns

### Simple GET

```java
// 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

```java
// 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

```java
// 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:

```java
// 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:

```java
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.

![Two panels split by a dashed line. On the left, in danger colour, three RestTemplate calls (get order, get invoice, create shipment) all funnel into a single ResponseErrorHandler set on the whole template, labelled one rule for every endpoint, so a 404 means the same thing everywhere. On the right, in accent colour, a RestClient builder with defaultStatusHandler provides client-wide error mapping and passes down to two calls: get order overrides it with onStatus for 404, which throws OrderNotFoundException, while get invoice keeps the default. A caption says error rules move next to the request they describe.](/writing/restclient-migration-error-handling.svg "A client-wide default plus per-call overrides lets a 404 mean different things on different endpoints, which is usually what the business meant all along.")

### Interceptors and customizers

Interceptors work the same way, registered on the builder:

```java
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:

```java
@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:

```java
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`:

```java
@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

1. **Upgrade** to Spring Boot 4.2 and list the deprecation warnings. That's your inventory.
2. **Add the bridge**: create a `RestClient` from each configured `RestTemplate`.
3. **Migrate call sites** service by service, starting with the simplest. Each one is a small, reviewable change.
4. **Rewrite customizers** as `RestClientCustomizer` beans.
5. **Move tests** to `RestTestClient` and bind `MockRestServiceServer` to the new builders.
6. **Delete** the `RestTemplate` beans 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.
---

Originally published at https://kishorek.dev/writing/resttemplate-deprecated-move-to-restclient.
© 2026 Kishore K Sharma. All rights reserved.
