OpenFeign / feign

Feign makes writing java http clients easier
Apache License 2.0
9.47k stars 1.92k forks source link

Response interceptor is not intercepting response #2503

Open mfanegg opened 2 months ago

mfanegg commented 2 months ago

(version 11.10)

Hey team,

I need to intercept a 409 so that my service sees it as a 200. From what I've scraped from the internet and the docs, you need to override aroundDecode, so my simple response interceptor looks something like this:

static class MyResponseInterceptor implements ResponseInterceptor {
    @Override
    public Object aroundDecode(final InvocationContext invocationContext) {
        final Response response = invocationContext.response();
        if (response.status() == 409) {
            final Response newResponse = response.toBuilder().status(200).build();
            return invocationContext.decoder().decode(newResponse, invocationContext.returnType());
        }
        return invocationContext.proceed();
    }
}

This doesn't actually do anything when I add it to my connector builder with .responseInterceptor(new MyResponseInterceptor()). I've tried using debugging and print statements, and it doesn't seem to actually hit my custom aroundDecode at all.

What am I doing wrong? Or perhaps I am misunderstanding the purpose of response interceptors?

Also, it would be fantastic if an example of a from-scratch response interceptor was added to the README. The README just tells us that it is possible, and gives us an example of a prebuilt response interceptor (side note, the RedirectionInterceptor that the README says Feign includes is not actually included with the package -- auto-import does not resolve it, and I'm unable to find it anywhere in version 11.10)

derklaro commented 2 months ago

The latest version is 13.3; this version actually includes the RedirectionInterceptor and the aroundDecode method does not exist (anymore?): https://github.com/OpenFeign/feign/blob/master/core/src/main/java/feign/ResponseInterceptor.java

Your use case should be doable using a custom response interceptor in 13.3. Just return the value you want from intercept in case of your 409 status or call chain.next(invocationContext) if you don't want to handle the response code. You can register multiple interceptors into one feign builder.

Maybe you're including the wrong version? Should be io.github.openfeign:<component>:13.3: https://mvnrepository.com/search?q=io.github.openfeign

mfanegg commented 2 months ago

@derklaro

Thank you for the response. I mentioned in my original post that we are using version 11.10. Do you know how to handle my use case in that version?

I know that we should update to 13.3 -- that work is being tracked separately. We're not ready to do that update right now.

derklaro commented 2 months ago

Ah, okay. I was just confused because you mentioned RedirectionInterceptor from the 13.3 readme, which is not mentioned in the 11.10 readme. I have no experience with the old version so I can not help, sorry.

mfanegg commented 2 months ago

Ah, my bad. I've been reading the 13.3 readme.

Upon reading the 11.10 readme, I see it does not appear to mention response interception at all. I suspect it isn't officially supported then / fleshed out, even if we're able to add them with responseInterceptor.

kdavisk6 commented 1 month ago

@mfanegg is correct. You will need to use a version of Feign with interceptor support.