Sub topic 14
When to choose HTTP interfaces over other options
This chapter assumes you have read the earlier chapters. You should know how to declare an @HttpExchange interface, build it with HttpServiceProxyFactory, and register it as a bean. It helps you pick between that approach and its neighbors.
The options
Four approaches cover most Spring Boot code that calls another HTTP service:
- HTTP interface: an annotated Java interface that Spring turns into a proxy.
- RestClient: a fluent, blocking client where you write each call by hand.
- WebClient: a fluent, reactive client built on Project Reactor.
- OpenFeign: an annotation-based client from the Spring Cloud ecosystem, using
@FeignClient.
RestTemplate also still exists. It is in maintenance mode, so avoid it in new code.
HTTP interfaces do not replace RestClient or WebClient. The proxy delegates every call to one of them through an adapter (RestClientAdapter or WebClientAdapter). Choosing an HTTP interface means choosing how you describe the API. Your RestClient or WebClient still sends the requests.
Comparison
| Concern | HTTP interface | RestClient | WebClient | OpenFeign |
|---|---|---|---|---|
| Style | Declarative | Imperative | Imperative, reactive | Declarative |
| Blocking | Yes, with `RestClient` | Yes | No | Yes |
| Annotations | Spring's own | None | None | Feign's own |
| Extra dependency | None | None | WebFlux | Spring Cloud |
| Per-call control | Limited | Full | Full | Limited |
Decision flow
When HTTP interfaces fit
Choose an HTTP interface when the remote API has several endpoints and the calls follow a regular pattern. The interface documents the API in one place. It is easy to mock in unit tests, because you can replace the interface with a Mockito mock. The same interface also works over RestClient or WebClient, so you can change the transport without touching callers.
When RestClient is better
Plain RestClient wins in these cases:
- You call one or two endpoints, so an interface adds ceremony.
- A call needs logic that annotations cannot express, such as choosing the URI or headers from runtime state, or branching on the response status.
- You need to inspect the response before deserializing it.
You can mix both styles. Keep the interface for routine calls and inject the RestClient for odd ones:
import org.springframework.web.client.RestClient;
class TodoGateway {
private final TodoClient todoClient;
private final RestClient restClient;
TodoGateway(TodoClient todoClient, RestClient restClient) {
this.todoClient = todoClient;
this.restClient = restClient;
}
Todo routine(long id) {
return todoClient.getTodo(id);
}
String rawBody(long id) {
return restClient.get()
.uri("/todos/{id}", id)
.retrieve()
.body(String.class);
}
}When to stay with OpenFeign
If your system already relies on Spring Cloud for service discovery, client-side load balancing, or circuit breakers wired through Feign, a migration costs more than it returns. Feign also uses its own annotation set and contract, so existing @FeignClient interfaces do not carry over directly. New projects without that infrastructure gain little from adding Spring Cloud just for a client.
Trade-offs to remember
- Hidden behavior. The proxy hides the HTTP call, so failures such as timeouts and 4xx errors look like ordinary method calls. Document which exceptions callers should expect.
- Blocking by default. With
RestClient, a call blocks the calling thread. UseWebClientand reactive return types only if the rest of your stack is reactive. - Cross-cutting rules still live on the client. Base URL, authentication, timeouts, and status handlers belong on the
RestClientor in group configuration, not on the interface.
Exercise
Your service calls a partner API with 12 stable endpoints, runs on Spring MVC (blocking), and does not use Spring Cloud. One endpoint needs its URI built from a value chosen at runtime. Which approach do you pick, and how do you handle the odd endpoint?
Show answer
Use an HTTP interface backed by RestClient for the 12 stable endpoints, because the interface documents the API in one place and is easy to mock. Handle the runtime-URI endpoint with a direct RestClient call, or with a URI argument on the interface method if that is enough. OpenFeign and WebClient add dependencies you do not need.