@FeignClient lets you call another HTTP service by writing a plain Java interface. You annotate the interface and its methods with Spring MVC annotations such as @GetMapping, and Spring Cloud OpenFeign generates the HTTP client at startup. It replaces hand-written RestTemplate or WebClient code with a declarative contract, and it plugs into service discovery, load balancing, timeouts and circuit breakers.
It is most useful in microservices where one service calls many others and you want those calls to read like local method calls without losing control over errors and timeouts.
Feign is an HTTP client binder originally built at Netflix. Spring Cloud OpenFeign wraps it so that the interface you write is picked up as a Spring bean. For every method, Feign builds the URL, serialises the request body, sends the request and deserialises the response with the same HttpMessageConverter infrastructure Spring MVC uses.
You get these benefits:
ResponseEntity plumbing unless you want it.Add the starter (the version comes from the Spring Cloud BOM that matches your Spring Boot release):
Then enable Feign client scanning on a configuration class:
Without @EnableFeignClients the annotated interfaces are ignored.
Suppose order-service needs user details from user-service. Define the contract as an interface:
Inject it like any other bean, preferably through the constructor:
Note that @PathVariable and @RequestParam need an explicit name on Feign methods unless you compile with -parameters.
The name attribute is the logical service name. With a discovery client on the classpath (Eureka, Consul, Kubernetes discovery), Spring Cloud LoadBalancer resolves that name to a list of instances and picks one per call, round-robin by default. Netflix Ribbon, which older tutorials mention, was removed from Spring Cloud; LoadBalancer is the replacement.
When you do not use discovery, set a fixed url:
In Kubernetes without a discovery client, a service DNS name such as http://user-service:8080 in url is a common choice.
Remote calls fail, so configure them. Timeouts are set per client or as a default, in milliseconds:
With spring.cloud.openfeign.circuitbreaker.enabled=true and a circuit breaker implementation such as Resilience4j on the classpath, each Feign method is wrapped in a circuit breaker. You can provide a fallback:
fallbackFactory is preferred over fallback because it gives you the cause of the failure for logging. Only return a fallback value when a degraded answer is acceptable to the caller.
Common extension points:
| Need | Mechanism |
|---|---|
| Add auth or tracing headers to every call | A RequestInterceptor bean |
| Map HTTP error responses to exceptions | A custom ErrorDecoder bean |
| See request and response details | logging.level for the client interface plus Logger.Level bean |
| Retries | A Retryer bean, or Resilience4j retry |
By default Feign throws FeignException for non-2xx responses, which has subclasses such as FeignException.NotFound. Catch those, or use an ErrorDecoder, instead of letting them leak as 500 errors.
Spring Framework 6 added declarative HTTP interface clients (@HttpExchange) built on RestClient or WebClient. They give a similar interface-based style without Spring Cloud. Feign remains a good choice when you already rely on Spring Cloud features such as discovery-aware load balancing and the Feign circuit breaker integration, or when an existing code base uses it widely. For new code, compare both and choose the one that fits your stack.
Not exactly. @FeignClient belongs to Spring Cloud OpenFeign, which integrates the open source OpenFeign library with Spring MVC annotations, Spring Boot configuration and Spring Cloud load balancing.
Usually @EnableFeignClients is missing, or the interface is outside the scanned packages. Set basePackages or clients on the annotation if needed.
OpenFeign is blocking. In a WebFlux application, prefer WebClient or an HTTP interface client backed by it, or isolate Feign calls on a bounded scheduler.
Register a RequestInterceptor bean that copies the header from the current request, or obtains a token for service-to-service calls.
@FeignClient turns an annotated interface into a working HTTP client that resolves service names through load balancing, applies configured timeouts and can fall back through a circuit breaker. Enable it with @EnableFeignClients, keep one interface per remote service, set explicit timeouts, and handle FeignException deliberately. For new projects, also evaluate Spring's built-in HTTP interface clients.