spring-cloud-openfeign icon indicating copy to clipboard operation
spring-cloud-openfeign copied to clipboard

FeignClientFactoryBean does not provide a way to register a ResponseMapper

Open celdridge91190 opened this issue 5 years ago • 5 comments

Feign provides an interface called ResponseMapper which allows you to do processing on a response before it is decoding. This is a useful extension for pre-processing things such as response headers without having to embed logic into your decoder. The FeignClientFactoryBean does not allow a mechanism to add one which forces consumers to abandon the auto configured feign client functionality and manually create the Feign client if they choose to take advantage of that feature. This request is simply to optionally allow a ResponseMapper to be exposed and included in the Feign client.

celdridge91190 avatar Nov 24 '20 18:11 celdridge91190

I think that using this customizer might give you the flexibility that you need

https://github.com/spring-cloud/spring-cloud-openfeign/pull/289

Unfortunately it is only available in the 2020.0 milestones at the moment. Maybe you could try the milestone build and let us know if it works for you? Then we can consider backporting it into Hoxton.

ryanjbaxter avatar Nov 30 '20 15:11 ryanjbaxter

I have actually already backported that to Hoxton: https://github.com/spring-cloud/spring-cloud-openfeign/issues/436. Please let us know if that can solve your issue.

OlgaMaciaszek avatar Nov 30 '20 16:11 OlgaMaciaszek

This looks like it should work! Is there a particular build deployed that I can use under 2.2.X to pull this in? The latest I've found so far is 2.2.6 and it isn't in there.

Thanks, Chris

On Mon, Nov 30, 2020 at 11:58 AM Olga Maciaszek-Sharma < [email protected]> wrote:

I have actually already backported that to Hoxton: #436 https://github.com/spring-cloud/spring-cloud-openfeign/issues/436. Please let us know if that can solve your issue.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/spring-cloud/spring-cloud-openfeign/issues/433#issuecomment-735911464, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAU6XMUKDWATTJSC63QBSB3SSPFMXANCNFSM4UBIQS2A .

celdridge91190 avatar Dec 07 '20 16:12 celdridge91190

@celdridge91190 not yet it will be in Hoxton.SR10

ryanjbaxter avatar Dec 07 '20 20:12 ryanjbaxter

The FeignBuilderCustomizer won't really help here. As the current method to set a ResponseMapper ( Feign.Builder.mapAndDecode(ResponseMapper mapper, Decoder decoder)) requires the Decoder.

This really needs to be handled in the FeignClientFactoryBean class.

Also it would be nice if Spring Cloud OpenFeign supplied a CompositeResponseMapper so that multiple ResponseMapper beans could be sourced, ordered and invoked in sequence.

As a workaround I currently have this in my @FeignClient(configuration=...) class.

    /**
     * Hack to work around the fact that Spring Cloud OpenFeign does not support any way to set a ResponseMapper.
     */
    @Bean
    public Decoder decoder(final ResponseMapper responseMapper, final ObjectFactory<HttpMessageConverters> messageConverters) {
        // this is the default decoder as per FeignClientsConfiguration#feignDecoder
        final Decoder defaultDecoder = new OptionalDecoder(
                new ResponseEntityDecoder(new SpringDecoder(messageConverters)));

        // here we delegate to the ResponseMapper before calling decode()
        return (response, type) -> defaultDecoder.decode(responseMapper.map(response, type), type);
    }

This essentially replicates what Feign.Builder.mapAndDecode() does.

aaronjwhiteside avatar Dec 09 '20 22:12 aaronjwhiteside