Video in progress

Senior 🌱 Spring

You need a declarative HTTP client for another service. How do you compare OpenFeign and Spring 6's @HttpExchange interfaces?

Both give you an HTTP client through an interface. @HttpExchange is part of Spring Framework itself, runs on top of RestClient/WebClient and needs no extra dependency; OpenFeign is tied to Spring Cloud and is now in maintenance mode only.

📖 Answer

The idea is the same in both: you write an interface and the framework builds the implementation.

@HttpExchange("/api/customers")
interface CustomerClient {
    @GetExchange("/{id}")
    CustomerDto get(@PathVariable Long id);

    @PostExchange
    CustomerDto create(@RequestBody NewCustomer body);
}

@Bean
CustomerClient customerClient(RestClient.Builder builder) {
    RestClient rc = builder.baseUrl("http://customer-service")
        .requestFactory(factoryWithTimeouts())
        .build();
    return HttpServiceProxyFactory.builderFor(RestClientAdapter.create(rc))
        .build().createClient(CustomerClient.class);
}

Comparison:

  • Dependency: @HttpExchange is just spring-web. Feign needs spring-cloud-openfeign and ties you to the Spring Cloud release train.
  • Future: the Spring Cloud team has declared OpenFeign "feature-complete" and recommends HTTP interfaces for new projects.
  • Core stack: @HttpExchange runs on RestClient (blocking) or WebClient (reactive), so timeout, interceptor and observability settings live in one place. Feign has its own encoder/decoder and client chain.
  • Conveniences: Feign has ready integration with Spring Cloud LoadBalancer and Resilience4j, and creates beans automatically with @FeignClient. With @HttpExchange you declare the bean yourself (this is getting simpler in Spring Framework 7 / Boot 4 too).

My choice: @HttpExchange + RestClient for new code. I wouldn't migrate existing Feign code for this reason alone: it keeps working.

Whichever you use, don't forget: connect/read timeouts, turning errors (4xx/5xx) into your own exceptions, retry only for idempotent requests, and metrics for every call.