Sub topic 6
Creating the proxy with HttpServiceProxyFactory
This chapter assumes you know Java 17 records and interfaces, basic Spring Boot (beans, @Configuration, dependency injection), HTTP status codes and JSON, and Spring MVC annotations such as @PathVariable. You have already seen how an interface describes a remote API. Here you turn that interface into a working object.
What the factory does
An @HttpExchange interface has no implementation. HttpServiceProxyFactory creates one at runtime. It reads the annotations on each method and builds a JDK dynamic proxy. When your code calls a method, the proxy turns the call into an HTTP request and hands it to an adapter. The adapter sends the request through a real HTTP client.
The factory does not send requests itself. It needs an adapter that wraps a client. For RestClient, the adapter is RestClientAdapter.
Building the proxy
The steps are always the same:
- Build a
RestClient, usually from Spring Boot's auto-configuredRestClient.Builder. - Wrap it with
RestClientAdapter.create(...). - Pass the adapter to
HttpServiceProxyFactory.builderFor(...)and callbuild(). - Call
createClient(...)with your interface type.
import org.springframework.web.client.RestClient;
import org.springframework.web.client.support.RestClientAdapter;
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
class ProxyDemo {
static TodoClient create() {
RestClient restClient = RestClient.builder()
.baseUrl("https://jsonplaceholder.typicode.com")
.build();
HttpServiceProxyFactory factory = HttpServiceProxyFactory
.builderFor(RestClientAdapter.create(restClient))
.build();
return factory.createClient(TodoClient.class);
}
}This example uses RestClient.builder() directly so it runs outside Spring. Inside a Spring Boot application, prefer the injected RestClient.Builder. Boot configures it with your message converters and any RestClientCustomizer beans, and a hand-made builder skips that setup.
The base URL lives on the RestClient, not on the interface. The interface holds only relative paths, such as /todos/{id}. This lets you point the same interface at different servers, for example in tests.
What the factory checks
createClient inspects every method on the interface. If a method has no exchange annotation, or an argument type the factory cannot bind, creation fails right away with an IllegalStateException or IllegalArgumentException. You learn about the mistake at startup instead of on the first call.
Methods that are not exchange methods, such as default methods, are not turned into HTTP calls. They run as ordinary interface code.
Common errors
| Symptom | Likely cause |
|---|---|
| Startup fails while creating the client | A method lacks an `@HttpExchange` variant, or has an argument type the factory cannot bind |
| Requests go to the wrong host or fail with "URI is not absolute" | The `RestClient` has no `baseUrl`, and the interface uses relative paths |
| Converters or customizations are missing | You used `RestClient.builder()` instead of the injected `RestClient.Builder` |
| `ClassCastException` on the proxy | You called `createClient` with a class, not an interface |
Reuse the factory
One factory can create many clients. If several interfaces share the same server, build the factory once and call createClient for each interface. If they talk to different servers, build one RestClient and one factory per server.
HttpServiceProxyFactory factory = HttpServiceProxyFactory
.builderFor(RestClientAdapter.create(restClient))
.build();
TodoClient todos = factory.createClient(TodoClient.class);The proxy is thread-safe as long as the underlying RestClient is. RestClient is, so you can share one proxy across requests.
Exercise
Why should a Spring Boot application build its RestClient from the injected RestClient.Builder instead of calling RestClient.builder()?
Show answer
The injected builder is auto-configured by Spring Boot. It carries the application's message converters and any RestClientCustomizer beans. RestClient.builder() creates a plain builder without that configuration.
Exercise
A proxy call fails with "URI is not absolute". The interface uses paths like /todos/{id}. What is missing, and where do you fix it?
Show answer
The RestClient has no base URL. Set it with baseUrl(...) on the builder before creating the adapter and factory.