Scrivio Interview library
CodingFoundations5 min read · updated 12 November 2026

Write a Spring HTTP interface client for GET /users/{id} returning User, and explain how it is turned into a working bean.

We declare an interface annotated with @HttpExchange("/users") and a method @GetExchange("/{id}") User get(@PathVariable long id), then ask HttpServiceProxyFactory, built on an adapter such as RestClientAdapter, to create a proxy that implements it. Each call on the proxy is turned into an HTTP request and executed by the underlying RestClient, WebClient or RestTemplate, so timeouts and error handling follow that client's configuration. Framework 7 adds group-based registration of these clients so we no longer write a factory bean per interface.

Why this question is asked

Most services call other services over HTTP, and for years that meant writing small wrapper classes around RestTemplate: build the URL, add the headers, call exchange(), map the response. HTTP interface clients, added in Spring Framework 6, let us describe the remote API as a Java interface and have Spring generate the implementation, much like Spring Data does for repositories. Interviewers ask for this in coding rounds because it is short to write but shows whether we understand what is generated and what still depends on the underlying client. In this answer, we will write the interface, turn it into a bean, follow a call through the proxy, look at error handling and timeouts, and see what Framework 7 changes.

1Writing the Interface

An HTTP interface is a plain Java interface. The type-level @HttpExchange sets a common path for every method, and each method carries an exchange annotation for its HTTP method, here @GetExchange. Method parameters use the same annotations we know from controllers, such as @PathVariable, @RequestParam, @RequestHeader and @RequestBody.

Java
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.service.annotation.GetExchange;
import org.springframework.web.service.annotation.HttpExchange;
@HttpExchange("/users")
public interface UserClient {
@GetExchange("/{id}")
User get(@PathVariable long id); // GET /users/42 -> User
}
public record User(long id, String name, String email) { }

Notice that the return type is just User. Spring reads the response body as JSON with the configured message converters. We could also return ResponseEntity<User> to see the status and headers, or void if we only care that the call succeeded.

2Turning the Interface Into a Bean

An interface cannot run on its own, so we need something that implements it. HttpServiceProxyFactory creates a proxy, an object generated at runtime that implements UserClient and handles every method call. The factory is built for an adapter, which is the bridge to the HTTP client that actually sends requests. Here we use RestClient, the synchronous client added in Framework 6.1.

Java
@Configuration
class UserClientConfig {
@Bean
UserClient userClient(RestClient.Builder builder) { // Boot provides a configured builder
RestClient restClient = builder.baseUrl("https://users.internal").build();
HttpServiceProxyFactory factory = HttpServiceProxyFactory
.builderFor(RestClientAdapter.create(restClient))
.build();
return factory.createClient(UserClient.class);
}
}
// Anywhere else:
User u = userClient.get(42); // sends GET https://users.internal/users/42
System.out.println(u.name()); // Ada

The adapter is the only part that changes between transports. WebClientAdapter.create(webClient) gives a reactive transport, where methods may return Mono<User>, and RestTemplateAdapter.create(restTemplate) lets an existing, already configured RestTemplate carry the calls. The interface itself stays the same.

3What Happens When We Call the Proxy

When we call get(42), the proxy looks up the metadata it prepared for that method: HTTP method GET and path /users/{id}. Argument resolvers then turn each parameter into part of the request, so the @PathVariable fills {id} with 42. The result is a description of the request, which the proxy hands to the adapter. The adapter executes it with RestClient, and the response body is converted into the declared return type.

Step throughOne call through an HTTP interface client
CallerUserClient proxyRestClientAdapterRestClientUsers service
get(42)
resolve arguments: GET /users/{id}, id=42
exchange request values
get().uri("/users/42").retrieve()
GET /users/42
200 {"id":42,"name":"Ada"}
User(42, Ada, ...)
User

8 messages in order. Play them, or step through them one at a time.

4Errors Follow the Underlying Client

The proxy adds no error handling of its own. With RestClient, the default status handler throws HttpClientErrorException for 4xx responses and HttpServerErrorException for 5xx responses, with specific subclasses such as HttpClientErrorException.NotFound. So the caller of userClient.get(99) sees the same exceptions it would see when using RestClient directly.

Java
try {
userClient.get(99);
} catch (HttpClientErrorException.NotFound e) {
System.out.println(e.getStatusCode()); // 404 NOT_FOUND
}
// To map errors in one place, configure the RestClient before building the proxy:
RestClient restClient = builder.baseUrl("https://users.internal")
.defaultStatusHandler(HttpStatusCode::is4xxClientError,
(request, response) -> { throw new UserServiceException(response.getStatusCode()); })
.build();

Because the status handler lives on the RestClient, it applies to every method of every interface built on that client, which is usually what we want.

5What Framework 7 Adds

Writing one factory bean per interface becomes tedious with many clients. Spring Framework 7 adds group-based registration: we declare which interfaces belong to a group, for example with an @ImportHttpServices annotation naming the group and the client types, and configure each group's underlying client once. Spring Boot 4 builds on this with configuration properties per group, such as a base URL. The exact annotation attributes and property names are new, so check the release notes for your version before relying on them.

6Follow-up questions

How do you set timeouts for an HTTP interface client?

On the underlying client, because the proxy only describes requests. For RestClient we configure its ClientHttpRequestFactory, for example JdkClientHttpRequestFactory with setReadTimeout(Duration.ofSeconds(2)) and an HttpClient built with a connect timeout, and pass it with requestFactory(...) on the builder. Spring Boot also offers properties for the connect and read timeout of auto-configured clients; check the property names for your Boot version. For WebClient, we set a response timeout on the Reactor Netty HttpClient. Every interface built on that client then inherits the same timeouts.

Can one interface be used with both RestClient and WebClient?

Yes, if its methods return plain types such as User or ResponseEntity<User>, because both adapters support blocking signatures; the WebClient adapter blocks for them. Methods that return Mono or Flux require a reactive adapter. In practice we choose one transport per application and keep the interface signatures consistent with it.

How do you test code that uses the client?

For unit tests, we mock the interface itself, which is much simpler than mocking RestClient. For the client's own behaviour, MockRestServiceServer can bind to a RestClient.Builder, or we run a small stub server such as WireMock and point the base URL at it.

7Conclusion

In this answer, we wrote an HTTP interface with @HttpExchange and @GetExchange, built it into a bean with HttpServiceProxyFactory and RestClientAdapter, and followed a call as the proxy resolves arguments into a request that RestClient executes. We saw that errors and timeouts come from the underlying RestClient, WebClient or RestTemplate, and that Framework 7 adds group-based registration so many clients can be set up without a factory bean each.

What to say in the interviewAbout 80 seconds aloud

Open with

I would declare an interface annotated with @HttpExchange("/users") and a method annotated with @GetExchange("/{id}") that returns User and takes a long id with @PathVariable. On its own that is just an interface, so in a configuration class I build a RestClient with the base URL, wrap it with RestClientAdapter.create, build an HttpServiceProxyFactory with builderFor that adapter, and call createClient(UserClient.class).

Then, if they want more

That returns a proxy implementing the interface, which I expose as a bean. When someone calls get(42), the proxy uses the annotations to build a GET to /users/42, the argument resolvers fill in path variables, query parameters, headers or the body, and the adapter executes it through RestClient and converts the JSON body into User.

The adapter is the only transport-specific part, so WebClientAdapter gives a reactive version and RestTemplateAdapter reuses an existing RestTemplate. Because the proxy adds nothing of its own, errors follow the underlying client: RestClient's default status handler throws HttpClientErrorException or HttpServerErrorException, and I can map them with defaultStatusHandler. Timeouts are set on the client's request factory too.

In Framework 7 there is group-based registration so I would not need a factory bean per interface, but I would check the exact API in the release notes.

About this answer

Last updated
12 November 2026
Versions it names
Spring Boot 4, Spring Framework 6, Spring Framework 7
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 "Write a Spring HTTP interface client for GET /users/{id} returning User, and explain how...": https://scrivio-interview-library.netlify.app/spring-boot/spring-framework-evolution/8885013685/