Scrivio Interview library
CodingFoundations6 min read · updated 12 November 2026

Write an OpenFeign client for GET /customers/{id} on the customers service with a 2 second read timeout, and explain how errors are handled by default.

We declare an interface with @FeignClient(name = "customers") and a method @GetMapping("/customers/{id}") Customer find(@PathVariable("id") long id), enable clients with @EnableFeignClients, and set spring.cloud.openfeign.client.config.customers.read-timeout=2000 with a connect-timeout as well. By default a non-2xx response throws a FeignException, which an ErrorDecoder can map to our own exceptions. Spring Cloud OpenFeign does not retry by default, because it registers Retryer.NEVER_RETRY, and circuit breaker support is switched on with spring.cloud.openfeign.circuitbreaker.enabled=true.

Why this question is asked

OpenFeign lets us call another service by writing only an interface, which is why so many Spring codebases use it. The interface is the easy part; the questions that matter in production are what happens when the service is slow and what happens when it answers with an error. Interviewers ask this coding question to check both. In this answer, we will write the client, set its timeouts, see what happens on a 404 or a 500 by default, map errors to our own exceptions, and look at retries and the circuit breaker.

1A Quick Recap of OpenFeign

Feign is a declarative HTTP client: we describe the remote API as a Java interface, and Feign generates an implementation at runtime that builds and sends the HTTP request for each method call. Spring Cloud OpenFeign integrates it with Spring, so we can use the familiar Spring MVC annotations such as @GetMapping and @PathVariable on the interface, Spring's message converters for JSON, and Spring Cloud LoadBalancer for service names.

2Writing the Client

We need the spring-cloud-starter-openfeign dependency, @EnableFeignClients on a configuration class so Spring scans for client interfaces, and the interface itself.

Java
@SpringBootApplication
@EnableFeignClients
public class BillingApplication { }
public record Customer(long id, String name, String email) { }
@FeignClient(name = "customers") // service name, resolved through LoadBalancer
public interface CustomerClient {
@GetMapping("/customers/{id}")
Customer find(@PathVariable("id") long id);
}
// in a service:
Customer c = customerClient.find(42); // GET http://<an instance of customers>/customers/42
System.out.println(c.name()); // Ada

Notice the explicit name in @PathVariable("id"). Spring MVC can often infer parameter names, but Feign interfaces are compiled separately and the name may not be available, so giving it explicitly avoids an error at startup. Because the client has a name and no url, the call goes through Spring Cloud LoadBalancer, which picks an instance of the customers service from discovery.

3Setting the Timeouts

Each Feign client has a connect timeout, the time allowed to open the connection, and a read timeout, the time allowed waiting for the response once connected. Without settings, Feign's defaults are generous: 10 seconds to connect and 60 seconds to read. A 60 second wait on a slow customers service would hold our threads far too long, so we set them per client, using the client's name as the key.

YAML
# application.yml
spring:
cloud:
openfeign:
client:
config:
customers: # matches @FeignClient(name = "customers")
connect-timeout: 500 # milliseconds
read-timeout: 2000 # milliseconds
default: # applies to every other client
connect-timeout: 1000
read-timeout: 5000
# customers takes 3 s to answer:
# feign.RetryableException: Read timed out executing GET http://customers/customers/42

A read timeout surfaces as a RetryableException, a subclass of FeignException that means the failure might succeed if tried again. Whether it is actually retried depends on the Retryer, which we come to below.

4What Happens on an Error Response

When the customers service answers with a status outside 2xx, Feign does not return null or an empty object. It passes the response to an ErrorDecoder. The default decoder throws a FeignException, using a subclass named for the status, such as FeignException.NotFound for 404 or FeignException.InternalServerError for 500. The exception carries the status and the response body.

Java
try {
customerClient.find(999);
} catch (FeignException e) {
System.out.println(e.status()); // 404
System.out.println(e.getClass()); // class feign.FeignException$NotFound
}
Step throughWhat happens to one call
  1. Send GET /customers/42connect 500 ms, read 2000 ms
  2. Timed out?
    YesRetryableExceptionNoResponse arrives
  3. Status 2xx?
    YesDecode JSON into CustomerNoErrorDecoder
  4. ErrorDecoder maps the status404 -> CustomerNotFoundException
  5. Retryerdefault NEVER_RETRY: the exception reaches the caller

5 steps in order. Play them, or step through them one at a time.

Letting FeignException escape into our business code ties that code to the HTTP client. A cleaner design maps errors to domain exceptions in one place with a custom ErrorDecoder, registered as a bean in a configuration class for this client.

5Mapping Errors to Domain Exceptions

Here is a decoder that turns a 404 into CustomerNotFoundException and lets the default decoder handle everything else.

Java
class CustomerClientConfig { // referenced from @FeignClient, not component-scanned
@Bean
ErrorDecoder customerErrorDecoder() {
ErrorDecoder fallback = new ErrorDecoder.Default();
return (methodKey, response) -> switch (response.status()) {
case 404 -> new CustomerNotFoundException(methodKey);
default -> fallback.decode(methodKey, response);
};
}
}
@FeignClient(name = "customers", configuration = CustomerClientConfig.class)
public interface CustomerClient { /* as before */ }
// customerClient.find(999) -> throws CustomerNotFoundException

The configuration class is deliberately not annotated with @Configuration in a scanned package; if it were, its beans would apply to every Feign client in the application. For a simple not-found case, Feign also has a dismiss404 option on the client that returns null or an empty Optional instead of throwing.

6Retries and the Circuit Breaker

Feign itself has a Retryer interface, but Spring Cloud OpenFeign registers Retryer.NEVER_RETRY by default, so a failed call is not retried. That is a sensible default: retrying a slow service multiplies its load, and only idempotent calls should be retried. If we want retries for this GET, we register a Retryer bean for the client, such as new Retryer.Default(100, 1000, 3), meaning start at 100 ms, back off up to 1 second, at most 3 attempts in total.

For a struggling dependency, a circuit breaker is the better tool. With a Spring Cloud CircuitBreaker implementation such as Resilience4j on the classpath and spring.cloud.openfeign.circuitbreaker.enabled=true, each Feign method call is wrapped in a circuit breaker. After too many failures the breaker opens and calls fail fast, or go to a fallback class named in @FeignClient(fallback = ...), instead of waiting 2 seconds each. When the service recovers, the breaker closes again.

7Follow-up questions

Why set the timeouts per client instead of only a default?

Because dependencies differ. A customers lookup should answer in milliseconds, while a report service may legitimately take longer. Per-client settings let each call have a timeout that matches its own latency, with a strict default for clients nobody configured.

Does the circuit breaker have its own timeout?

Yes. Resilience4j's time limiter, used by the circuit breaker integration, has a default of 1 second in many setups, which would cut our 2 second read timeout short. We align the two, either by raising the time limiter for this client or by disabling it and relying on Feign's read timeout (check your version for the property names).

How would you test this client?

With a stub server such as WireMock: point the client's url at the stub in a test, return a 404 and assert CustomerNotFoundException, add a 3 second delay and assert the timeout exception, and return a normal JSON body and assert the mapped Customer record.

8Conclusion

In this answer, we wrote an OpenFeign client with @FeignClient and @GetMapping, enabled it with @EnableFeignClients, and set a 2 second read timeout and a short connect timeout per client. We saw that non-2xx responses throw a FeignException by default, that an ErrorDecoder maps them to domain exceptions, that Spring Cloud OpenFeign does not retry because it uses Retryer.NEVER_RETRY, and that the circuit breaker is switched on with spring.cloud.openfeign.circuitbreaker.enabled=true.

What to say in the interviewAbout 80 seconds aloud

Open with

I would enable Feign with @EnableFeignClients and write an interface annotated @FeignClient with name customers, with a method annotated @GetMapping for /customers/{id}, returning Customer and taking a long id with @PathVariable naming id explicitly.

Then, if they want more

Because it has a name and no url, calls go through Spring Cloud LoadBalancer to an instance of the customers service. For timeouts, I would set them per client under spring.cloud.openfeign.client.config.customers: read-timeout 2000 and connect-timeout something short like 500 milliseconds, and a strict default for other clients, since Feign's own defaults are 10 seconds to connect and 60 to read.

A read timeout comes back as a RetryableException. For errors, by default any non-2xx response goes to the ErrorDecoder, and the default one throws a FeignException subclass such as FeignException.NotFound for 404. I prefer a custom ErrorDecoder in the client's configuration class that maps, say, 404 to a CustomerNotFoundException, so business code does not depend on Feign types.

Retries: Spring Cloud OpenFeign registers Retryer.NEVER_RETRY, so nothing is retried unless I add a Retryer bean, which I would only do for idempotent calls like this GET. For a failing dependency I would rather enable the circuit breaker with spring.cloud.openfeign.circuitbreaker.enabled=true and Resilience4j, with a fallback, making sure its time limiter does not cut the 2 second read timeout short.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Write an OpenFeign client for GET /customers/{id} on the customers service with a 2...": https://scrivio-interview-library.netlify.app/spring-boot/spring-cloud/bf0d08c282/