Scrivio Interview library
ScenarioIntermediate6 min read · updated 12 November 2026

Your codebase has 300 RestTemplate call sites with custom interceptors and error handlers. Plan a migration to RestClient that does not stop feature work.

We migrate gradually, never as a freeze. First we build RestClient from the existing RestTemplate configuration with RestClient.builder(restTemplate), so request factories, interceptors and converters are reused unchanged, and we keep all shared configuration (timeouts, observability, auth) in one builder bean. Then teams convert call sites module by module as they touch them, testing 4xx and 5xx paths because error handling moves to onStatus, introduce HTTP interface clients for the most-called services, and track the remaining usages with a static check while ArchUnit or code review blocks new RestTemplate code.

Why this question is asked

Three hundred call sites spread over many modules is too much to migrate in one go, and no business will stop feature work for a client library change. Yet leaving RestTemplate in place forever means a rushed migration when it is finally removed. Interviewers ask this scenario to see whether we can plan a large mechanical change that runs alongside normal work. In this answer, we will take an inventory, make RestClient reuse the existing setup, centralise configuration, migrate module by module while watching the error-handling differences, add HTTP interfaces where they pay off, and stop new RestTemplate code from appearing while we work.

1Taking an Inventory First

Before planning, we need to know what we have. A search for RestTemplate usages, grouped by module and by the downstream service called, gives us the shape of the work. We also list the custom pieces: every ClientHttpRequestInterceptor (for auth headers, logging, correlation ids), every ResponseErrorHandler, and how many RestTemplate beans exist and with what settings.

Typically the picture is uneven: perhaps 200 of the 300 call sites go to five frequently used services, and the rest are scattered. That tells us where HTTP interfaces will save the most code, and which modules can be migrated almost mechanically.

2Reusing the Existing Setup With RestClient.builder(restTemplate)

The key to a low-risk migration is that RestClient can be built from an existing RestTemplate. It then uses the same request factory (so the same connection pool and timeouts), the same interceptors, the same message converters and the same URI template handler. So a migrated call sends exactly the same HTTP request as before.

Java
@Configuration
class HttpClientsConfig {
@Bean
RestTemplate paymentsRestTemplate(RestTemplateBuilder builder, AuthInterceptor auth) {
return builder.rootUri("https://payments.internal")
.connectTimeout(Duration.ofSeconds(2)).readTimeout(Duration.ofSeconds(5))
.additionalInterceptors(auth)
.errorHandler(new PaymentsErrorHandler())
.build();
}
@Bean
RestClient paymentsRestClient(RestTemplate paymentsRestTemplate) {
return RestClient.builder(paymentsRestTemplate).build(); // same factory, interceptors, converters
}
}

Notice that the configuration stays in one place, the existing RestTemplate bean, during the transition. Both old and new call sites for payments share it, so a timeout change applies to both. Later, when no RestTemplate call sites are left for payments, we move the configuration onto a RestClient.Builder and delete the template.

3Migrating Call Sites and Watching Error Handling

Most call sites translate directly. The part that needs care is errors. RestTemplate calls a ResponseErrorHandler on every response; RestClient applies status handlers in retrieve(), registered with onStatus per call or as defaults on the builder. When built from a RestTemplate, RestClient adapts the template's error handler, but the details differ, for example exchange() does not apply status handlers at all, and a per-call onStatus takes precedence. Check your version's behaviour and test it.

Java
// Before
try {
PaymentDto p = paymentsRestTemplate.getForObject("/payments/{id}", PaymentDto.class, id);
} catch (HttpClientErrorException.NotFound e) {
return Optional.empty();
}
// After
PaymentDto p = paymentsRestClient.get().uri("/payments/{id}", id)
.retrieve()
.onStatus(s -> s.value() == 404, (req, res) -> { throw new PaymentNotFound(id); })
.body(PaymentDto.class);
// tests per migrated client: 200 -> PaymentDto, 404 -> PaymentNotFound, 503 -> HttpServerErrorException

So each migrated client gets tests for its success path, its expected 4xx responses and a 5xx response, using MockRestServiceServer or WireMock. That is cheap to write and catches the cases where old code relied on a quirk of the custom error handler.

We migrate module by module, ideally when a team is working in that module anyway, and keep each change small enough to review in minutes. No feature freeze is needed, because old and new calls coexist safely on the same configuration.

4Introducing HTTP Interfaces for the Busiest Services

For the handful of services that account for most call sites, we go further than a one-to-one translation. An HTTP interface turns twenty scattered calls with URI strings into one typed interface that all callers share.

Java
@HttpExchange("/payments")
interface PaymentsClient {
@GetExchange("/{id}")
PaymentDto get(@PathVariable String id);
@PostExchange
PaymentDto create(@RequestBody CreatePayment request);
}
@Bean
PaymentsClient paymentsClient(RestClient paymentsRestClient) {
return HttpServiceProxyFactory.builderFor(RestClientAdapter.create(paymentsRestClient))
.build().createClient(PaymentsClient.class);
}

Callers now depend on PaymentsClient rather than on any HTTP client API. That also protects us from the next migration: if Spring's client changes again, only the bean definition changes.

5Tracking Progress and Blocking New Usages

A migration that runs for months needs two guardrails. The first is a ratchet: new code must not add RestTemplate usages. An ArchUnit rule in the test suite does this, frozen so that existing violations are recorded and only new ones fail the build. The second is visibility: a small report in CI counts the remaining usages per module, so progress is shown on a dashboard and nobody has to guess.

Java
@ArchTest
static final ArchRule noNewRestTemplate = FreezingArchRule.freeze(
noClasses().that().resideOutsideOfPackage("..config..")
.should().dependOnClassesThat().areAssignableTo(RestTemplate.class));
// existing 300 usages stored as known violations; a new usage in a feature branch fails the build
Phases of the migration
PhaseWorkFeature work affected?
InventoryCount call sites, interceptors, error handlersNo
BridgeRestClient.builder(restTemplate) beans, shared configNo
GuardrailFrozen ArchUnit rule, usage count in CINo
MigrateModule by module on touch, error-path testsSmall, reviewed PRs
DeclarativeHTTP interfaces for the busiest servicesPer service
FinishMove config to RestClient.Builder, delete templatesNo

With the freeze in place, the number can only go down. Each time a team migrates a module, the stored violations shrink, and once the count reaches zero we delete the RestTemplate beans and the rule becomes a plain rule.

6Follow-up questions

Could we automate the call-site changes?

Partly. OpenRewrite and similar tools can do mechanical rewrites, and they are worth trying on the simple getForObject cases, but calls that depend on custom error handling or exchange() with complex types still need a human and tests. Automation saves time; the error-path tests are still required.

How long should the migration take?

With migration on touch plus a few focused sprints for modules nobody touches, a codebase of 300 call sites typically finishes in a few quarters. The guardrail matters more than the speed: as long as the count only goes down, the work is under control.

7Conclusion

In this answer, we planned a migration of 300 RestTemplate call sites to RestClient without stopping feature work. We inventoried usages and custom pieces, built RestClient beans from the existing templates with RestClient.builder(restTemplate) so factories, interceptors and converters stay the same, kept shared configuration in one place, migrated module by module with tests for 4xx and 5xx paths because error handling now uses onStatus, introduced HTTP interfaces for the busiest services, and used a frozen ArchUnit rule and a usage count to block new RestTemplate code and track progress.

What to say in the interviewAbout 80 seconds aloud

Open with

I would run it as a gradual migration alongside feature work. First an inventory: call sites per module and per downstream service, plus every custom interceptor and error handler.

Then, if they want more

Then I bridge: for each existing RestTemplate bean, I create a RestClient with RestClient.builder(restTemplate), which reuses the same request factory, interceptors and message converters, so migrated calls send identical requests and timeouts, observability and auth stay in one configuration. Teams then migrate module by module, ideally when they touch the code anyway, in small pull requests.

The main risk is error handling: RestTemplate uses a ResponseErrorHandler, RestClient uses onStatus handlers in retrieve, and the adaptation is not identical in every case, so each migrated client gets tests for success, expected 4xx and a 5xx.

For the few services that account for most calls, I introduce HTTP interface clients, so callers depend on a typed interface instead of a client API, which also makes the next migration local.

To keep it under control, I add a frozen ArchUnit rule so no new RestTemplate usage can be added while existing ones are tracked, and a CI count per module. When it reaches zero, I move the configuration to RestClient.Builder beans and delete the templates.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Your codebase has 300 RestTemplate call sites with custom interceptors and error...": https://scrivio-interview-library.netlify.app/spring-boot/http-clients/64afd3471e/