Search code examples
javaspring-bootfeign

How to intercept http response in feign and spring boot before ErrorDecoder invoked


I need to intercept HTTP response and alter HTTP status code. for example, I might change 200 to 400 (based on the response object itself) so that the ErrorDecoder should be invoked.

I need to do this without extending the feign.Client class! (in fact, the feign.httpclient.ApacheHttpClient which I am using is final!)

I tried to provide an implementation for ResponseMapper but it seems the response mapper (and the Decoder that I used to override) isn't invoked until the HTTP status check is done inside AsyncResponseHandler#handleResponse!

I am talking mainly here about how the implementation of SynchronousMethodHandler and AsyncResponseHandler works.

To sum it up, my main issue is that AsyncResponseHandler#handleResponse doesn't invoke the Decoder (hence the ResponseMapper) before the ErrorDecoder, and if it does then the ResponseMapper can do the HTTP status code I need.


Solution

  • To override this shortcoming, I'd to refere to the implementation class in the code (instead of depending on the auto configuration of spring boot)

    @Bean
    public Client feignClient(HttpClient httpClient) {
        var client = new ApacheHttpClient(httpClient);
        return (request, options) -> convertResponse(client.execute(request, options));
    }
    
    private Response convertResponse(Response response) throws IOException {
    
        // ...
    }