Sub topic 7
Exposing the client as a bean
This chapter assumes you can write an @HttpExchange interface and build a proxy with HttpServiceProxyFactory. It also assumes you know Spring Boot basics: @Configuration, @Bean, and constructor injection. Here you turn the generated proxy into a normal Spring bean that other classes can inject.
Why a bean?
HttpServiceProxyFactory.createClient returns an object that implements your interface. Spring does not know about that object until you register it. If you build it inside a service class, you repeat the setup everywhere and make testing harder. Registering it as a bean gives you one configured instance, constructor injection, and an easy place to swap it in tests.
Registering the proxy with a @Bean method
The usual approach is a @Configuration class with one @Bean method per client. Spring Boot auto-configures a RestClient.Builder, so you can ask for it as a method parameter.
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestClient;
import org.springframework.web.client.support.RestClientAdapter;
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
@Configuration
class TodoClientConfig {
@Bean
TodoClient todoClient(RestClient.Builder builder) {
RestClient restClient = builder
.baseUrl("https://jsonplaceholder.typicode.com")
.build();
HttpServiceProxyFactory factory = HttpServiceProxyFactory
.builderFor(RestClientAdapter.create(restClient))
.build();
return factory.createClient(TodoClient.class);
}
}The steps in the method are:
- Spring injects the auto-configured
RestClient.Builder. - You set the base URL and call
build()to get aRestClient. RestClientAdapter.createwraps theRestClientso the factory can use it.createClientgenerates the implementation ofTodoClient.
The bean type is the interface, not the generated class. Callers depend only on TodoClient.
Injecting and using the client
Once the bean exists, you inject it like any other dependency.
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;
@Service
class TodoService {
private final TodoClient todoClient;
TodoService(TodoClient todoClient) {
this.todoClient = todoClient;
}
Todo findOrNull(long id) {
try {
return todoClient.getTodo(id);
} catch (HttpClientErrorException.NotFound e) {
return null;
}
}
}TodoService has no HTTP code in it. It calls a method on an interface. The try/catch is there because RestClient throws HttpClientErrorException subclasses for 4xx responses by default. Error handling gets its own chapter.
Several clients
Each remote API usually needs its own base URL and settings. Write one @Bean method per client. The RestClient.Builder bean is prototype-scoped, so every injection point receives a fresh builder. Changing the base URL for one client does not affect another.
If you repeat the factory code often, extract a small helper method in the configuration class. It takes a RestClient and an interface type, and it returns the proxy.
The shortcut in Spring Framework 7 and Boot 4
Newer versions can register the interface for you, with no factory code.
import org.springframework.context.annotation.Configuration;
import org.springframework.web.service.registry.ImportHttpServices;
@Configuration
@ImportHttpServices(group = "todos", types = TodoClient.class)
class HttpClientsConfig {
}The base URL then comes from configuration.
spring.http.serviceclient.todos.base-url=https://jsonplaceholder.typicode.comThe group name links the annotation to the property prefix. A later chapter covers groups in detail. Use the manual @Bean approach on older versions or when you need full control of the RestClient.
Common errors
- No qualifying bean of type
TodoClient. The configuration class is not component-scanned, or you never declared the@Bean. Check that it sits under your application's base package. - Requests go to the wrong host. You forgot
baseUrl(...), or the interface path is relative and no base URL was set. - Two beans of the same interface type. If you register the same interface twice with different settings, inject with
@Qualifieror give the@Beanmethods distinct names and match the parameter name. RestClient.Buildernot found. The project lacks the web starter. Addspring-boot-starter-web, or build aRestClientwithRestClient.builder().
Exercise
Why is it safe for two @Bean methods to each take a RestClient.Builder parameter and call baseUrl(...) with different values?
Show answer
Spring Boot's RestClient.Builder is prototype-scoped, so each injection point gets its own fresh builder. Setting a base URL on one builder does not change the other.
Exercise
Which Spring versions can use @ImportHttpServices, and what links the annotation to the spring.http.serviceclient properties?
Show answer
Spring Framework 7 and Spring Boot 4. The group attribute of the annotation must match the group name used in the property key, for example todos in spring.http.serviceclient.todos.base-url.