Sub topic 12
Registering clients by group in Spring Framework 7 and Boot 4
This chapter assumes you can write an @HttpExchange interface and have built a proxy by hand with HttpServiceProxyFactory, as covered earlier. It needs Spring Framework 7 and Spring Boot 4. Earlier versions do not have the annotations and properties shown here.
The problem with manual factories
Each client so far needed a @Bean method that built a RestClient, wrapped it in an adapter, created a factory, and called createClient. With three or four remote APIs, that code repeats with only the base URL and interface changing.
Spring Framework 7 removes this by letting you declare which interfaces belong together in a named group. Spring builds one client per group and creates the proxies for you.
Declaring a group
Put @ImportHttpServices on a configuration class. The group names the group, and types lists the interfaces to register.
import org.springframework.context.annotation.Configuration;
import org.springframework.web.service.registry.ImportHttpServices;
@Configuration
@ImportHttpServices(group = "todos", types = TodoClient.class)
class HttpClientsConfig {
}Each listed interface becomes a bean, so TodoService can inject TodoClient as before. You no longer write a factory or call createClient.
The annotation is repeatable, so one configuration class can declare several groups. Instead of types, you can use basePackages to scan a package for interfaces that carry @HttpExchange.
Configuring a group with properties
Boot 4 reads settings from spring.http.serviceclient.<group>. Every client in the group shares them.
spring.http.serviceclient.todos.base-url=https://jsonplaceholder.typicode.comThe group name in the property key must match the group attribute. A mismatch does not fail at startup. The group simply gets no base URL, and requests fail later with confusing errors about relative URIs.
The same prefix also accepts connection and read timeouts and default headers, for example connect-timeout, read-timeout, and default-header.<name>. Check the Boot 4 reference for the exact property names in your version.
How the pieces fit
Why groups
Interfaces in the same group share one underlying RestClient, so they share a base URL, headers, and timeouts. Put interfaces in the same group only when they call the same remote service. A todo API and a payment API need different base URLs and credentials, so they belong in separate groups.
| Concern | Manual factory | Group registration |
|---|---|---|
| Proxy creation | Your `@Bean` code | Spring |
| Base URL | `builder.baseUrl(...)` | `spring.http.serviceclient.<group>.base-url` |
| Shared settings | Repeated per bean | One group |
| Custom logic | Full control | Needs a customization hook |
Common errors
- Group name mismatch. The
groupvalue and the property key differ in spelling or case, so settings are silently not applied. - Mixing APIs in one group. A shared base URL cannot be correct for two unrelated services.
- Using the pattern on Boot 3.
@ImportHttpServicesand thespring.http.serviceclientproperties do not exist there. Use the manual factory. - Needing logic that properties cannot express. Authentication that computes a token per request, for example, cannot be set with a property. Spring provides group configurer callbacks, such as
RestClientHttpServiceGroupConfigurer, to customize the underlying client. If you need full control of one client, keep the manual@Beanfor that client only. The two styles can coexist.
Exercise
Your application calls two APIs: orders at https://orders.example.com and billing at https://billing.example.com. You have an OrderClient and a BillingClient interface. How do you register both with group registration, and what property keys set their base URLs?
Show answer
Declare two groups, either as two @ImportHttpServices annotations on one configuration class or on two classes: @ImportHttpServices(group = "orders", types = OrderClient.class) and @ImportHttpServices(group = "billing", types = BillingClient.class). Then set spring.http.serviceclient.orders.base-url=https://orders.example.com and spring.http.serviceclient.billing.base-url=https://billing.example.com. They must be separate groups because each group shares one base URL.