Search code examples
javanetflix-feign

Exception Handling in Netflix Feign


I am using Netflix Feign to call HTTP Request as below -

@Headers("Content-Type: application/json; Accept: application/json")
public interface EmployeeClient {

  @RequestLine("POST")
  @Headers("client-id: TEST")   
  EmployeeResponse employee(EmployeeRequest employeeRequest);
}

How can I handle Exceptions here if service throw an error or service is not reachable or down. How can I use ErrorDecoder?


Solution

  • I suppose you are using Netflix Feign standalone not with spring boot.

    You can easily integrate a custom ErrorDecoder while building Feign client.

    Feign.builder().errorDecoder(new MyCustomErrorDecoder())
                     .target(MyApi.class, "https://api.hostname.com");
    

    For more details please look into official documentation here