Search code examples
javaresteasyquarkus

Resteasy error response body is unreachable


I am using Quarkus framework with RestEasy to REST communication. Everything works great when response code is 200 etc. When client gets an error code e.g. 400 Bad Request resteasy returns WebApplicationException and I can`t reach response body.

MyService.java

@Path("/")
@RegisterRestClient
@RegisterProvider(value = ClientLoggingFilter.class, priority = 100)
public interface MyService {

    @POST
    @Path("/foo")
    @Consumes(MediaType.APPLICATION_JSON)
    MyRespnse create(MyRequest request);

I`ve been trying to read entity from WebApplicationException but entity is always null. In Postman service returns body like:

{
   "error" : {
      "id" : "id does not exist"
      }
}

Solution

  • Look into http://download.eclipse.org/microprofile/microprofile-rest-client-1.0/apidocs/org/eclipse/microprofile/rest/client/ext/ResponseExceptionMapper.html

    @Provider
    public class CustomResponseExceptionMapper implements ResponseExceptionMapper<RuntimeException> {
        public CustomResponseExceptionMapper () {
        }
    
        public boolean handles(int statusCode, MultivaluedMap<String, Object> headers) {
    
        }
    
        public CusomExceptionMapper toThrowable(Response response) {
            try {
                String responseString = (String)response.readEntity(String.class);
             ............
            }
        }
    }
    

    Or

        public class CustomExceptionMapper
                implements ResponseExceptionMapper<Throwable> {
    }
    

    Register the ResponseExceptionMapper Provider:

    @Path("/xyz")
    @RegisterProvider(CustomResponseExceptionMapper.class)
    @RegisterRestClient
    @Timeout