4

I am using Feign Client,

I have a Location service. So I created a client for my LocationService using FeignClient.

@FeignClient(url="http://localhost:9003/location/v1", name="location-service")
public interface LocationControllerVOneClient {

    @RequestMapping(value = "/getMultipleLocalities", method = RequestMethod.POST)
    public Response<Map<Integer, Locality>> getMultipleLocalities(List<Integer> localityIds);

    @RequestMapping(value = "/getMultipleCities", method = RequestMethod.POST)
    public Response<Map<Integer, City>> getMultipleCities(List<Integer> cityIds);

    @RequestMapping(value = "/getMultipleStates", method = RequestMethod.POST)
    public Response<Map<Integer, State>> getMultipleStates(List<Integer> stateIds);

    @RequestMapping(value = "/getMultipleCitiesName", method = RequestMethod.POST)
    public Response<Map<Integer, String>> getMultipleCitiesName(MultiValueMap<String, String> formParams);

    @RequestMapping(value = "/getMultipleStatesName", method = RequestMethod.POST)
    public Response<Map<Integer, String>> getMultipleStatesName(MultiValueMap<String, String> formParams);

    @RequestMapping(value = "/getMultipleLocalitiesName", method = RequestMethod.POST)
    public Response<Map<Integer, String>> getMultipleLocalitiesName(MultiValueMap<String, String> formParams);

}

Now other services might call this LocationService via LocationClient.

I want to do exception handling for this Feign Client(LocationClient) at a common place(i.e. I just donot want each caller to do this. This should be part of LocationClient). Exception Could be connection refused(if LocationService is down), timeout etc.

Ankit Bansal
  • 2,162
  • 8
  • 42
  • 79

2 Answers2

2

You could use a feign ErrorDecoder for exception handling. Below is the url for your reference.

https://github.com/OpenFeign/feign/wiki/Custom-error-handling

Example :

public class MyErrorDecoder implements ErrorDecoder {

    private final ErrorDecoder defaultErrorDecoder = new Default();

    @Override
    public Exception decode(String methodKey, Response response) {
        if (response.status() >= 400 && response.status() <= 499) {
            return new MyBadRequestException();
        }
        return defaultErrorDecoder.decode(methodKey, response);
    }

}

To get this ErrorDecoder you need create a bean for it as below :

@Bean
public MyErrorDecoder myErrorDecoder() {
  return new MyErrorDecoder();
}
Saurabh Oza
  • 169
  • 4
  • 18
  • How should the name of the bean be? And who will get the bean according to the name? I only see ways of doing it with `openfeign`, not feign client of Spring as bean – WesternGun Jun 28 '19 at 08:56
1

You can define a fallback client that is called when an exception like timeout or connection refused comes up:

@FeignClient(url="http://localhost:9003/location/v1", name="location-service", fallback=LocationFallbackClient.class)
public interface LocationControllerVOneClient {
    ...
}

LocationFallbackClient must implement LocationControllerVOneClient.

Tom
  • 3,913
  • 19
  • 28