Search code examples
interceptorquarkusquarkus-rest-client

In Quarkus, is there a way to apply a Response Interceptor on the REST client calls?


I have a product REST API I need to use that returns things like:
HTTP 200 OK
{ "return_code": "failure_x" }

I cannot change that but I need to keep my project sane.

Is there a Quarkus REST Client Response Interceptor I can use?
It seems to me the interceptors are for my controllers and not for my clients.

I want to intercept the response and modify it to the the proper HTTP code and then process it.


Solution

  • As karelss pointed out in the comments, you can create a ClientResponseFilter to do what you want.

    Such a filter can be registered by:

    • @RegisterProvider(MyFilter.class) on your JAX-RS interface
    • in application.properties with:
    com.example.MyInterface/mp-rest/providers=com.example.MyFilter
    

    Another option is to create a custom ResponseExceptionMapper. You register it in the same way as the filter. This is how an example exception mapper looks like:

    package io.quarkus.rest.client.reactive.runtime;
    
    import javax.ws.rs.WebApplicationException;
    import javax.ws.rs.core.MultivaluedMap;
    import javax.ws.rs.core.Response;
    
    import org.eclipse.microprofile.rest.client.ext.ResponseExceptionMapper;
    
    public class DefaultMicroprofileRestClientExceptionMapper implements ResponseExceptionMapper {
    
        public Throwable toThrowable(Response response) {
            try {
                response.bufferEntity();
            } catch (Exception var3) {
            }
    
            return new WebApplicationException("Unknown error, status code " + response.getStatus(), response);
        }
    
        public boolean handles(int status, MultivaluedMap headers) {
            return status >= 400;
        }
    
        public int getPriority() {
            return 5000;
        }
    }
    

    For more info on exception mappers, take a look at the MicroProfile Rest Client specification: https://download.eclipse.org/microprofile/microprofile-rest-client-2.0/microprofile-rest-client-spec-2.0.html#_responseexceptionmapper