The @FeignClient annotation, from Spring Cloud OpenFeign, turns a Java interface into an HTTP client for another service. You declare methods with Spring MVC annotations such as @GetMapping, and Spring generates the implementation, so calling inventory.find(42) performs a real HTTP request.
It removes the boilerplate of building requests with RestTemplate or WebClient, and it plugs into Spring Cloud service discovery, client-side load balancing and circuit breakers.
@FeignClient does@FeignClient doesFeign is a declarative HTTP client. Spring Cloud OpenFeign scans for interfaces annotated with @FeignClient, creates a proxy bean for each one, and translates method calls into requests using the mapping annotations on the interface. JSON is encoded and decoded with the application's HttpMessageConverters.
Because the proxy is a normal Spring bean, you inject it like any other dependency and mock it easily in tests.
Add the starter to a Spring Boot project that uses the Spring Cloud BOM matching your Boot version:
Then enable client scanning on a configuration class:
Inject and use it:
Parameter names on @PathVariable and @RequestParam must be written out explicitly, because Feign does not infer them reliably.
| Attribute | Meaning |
|---|---|
name or value | Logical client name; also the service id used for load balancing when no url is given |
url | Fixed base URL, which bypasses service discovery |
path | Prefix added to every method mapping |
contextId | Distinguishes several clients that share the same name |
configuration | Class with custom Feign beans for this client only |
fallback | Class implementing the interface, used when a call fails |
fallbackFactory | Factory that also receives the cause of the failure |
primary | Whether the proxy is marked primary (default true) |
If you omit url, the name is treated as a service id. Spring Cloud LoadBalancer resolves it through the configured DiscoveryClient, such as Eureka, Consul or Kubernetes, and spreads calls across instances.
Netflix Ribbon and Hystrix were removed from Spring Cloud in the 2020.0 release train. Spring Cloud LoadBalancer and Spring Cloud CircuitBreaker (with Resilience4j) replace them, so older tutorials that mention Ribbon or feign.hystrix.enabled no longer apply.
Configure connection and read timeouts per client or as a default:
Enable the circuit breaker integration and add a fallback:
Fallbacks must be Spring beans. By default, a non-2xx response throws a FeignException with the status in status(). Register a custom ErrorDecoder bean to translate statuses into your own exceptions, for example a 404 into NotFoundException.
Never retry non-idempotent calls blindly. Feign's default retryer is disabled in Spring Cloud OpenFeign, and any retry policy you add should be limited to safe requests.
Set logger-level to basic, headers or full, and raise the log level of the interface package to DEBUG:
To add headers such as an authorisation token to every call, declare a RequestInterceptor bean in the client's configuration class.
Spring Framework 6 added HTTP interface clients (@HttpExchange), which give a similar declarative style on top of RestClient or WebClient without an extra dependency. Spring Cloud OpenFeign remains widely used and supported, especially where you already depend on its discovery and circuit breaker integration. For new projects with no Spring Cloud requirements, compare both.
name and url in @FeignClient?name identifies the client and, without url, the service resolved through discovery. url points directly to a fixed address and skips discovery.
@FeignClient?No. With a url, Feign calls that address directly. Discovery is only needed when you rely on service ids.
Usually @EnableFeignClients is missing, or the interface is outside the scanned packages. Set basePackages or clients on the annotation.
Yes. Standard Feign clients make blocking calls. For a reactive stack use WebClient or HTTP interface clients on top of it.
@FeignClient generates HTTP clients from annotated Java interfaces. Enable it with @EnableFeignClients, declare methods with Spring MVC annotations, pick url or a service name, and configure timeouts, error decoding and fallbacks. Use Spring Cloud LoadBalancer and Resilience4j in current releases, and consider @HttpExchange for projects that do not need Spring Cloud.