I have a simple client using RESTEasy as follows:
public class Test {
public static void main(String[] args) {
ResteasyClient client = new ResteasyClientBuilder().build();
ResteasyWebTarget target = client.target("http://localhost");
client.register(new MyMapper());
MyProxy proxy = target.proxy(MyProxy.class);
String r = proxy.getTest();
}
}
public interface MyProxy {
@GET
@Path("test")
String getTest();
}
@Provider
public class MyMapper implements ClientExceptionMapper<BadRequestException>{
@Override
public RuntimeException toException(BadRequestException arg0) {
// TODO Auto-generated method stub
System.out.println("mapped a bad request exception");
return null;
}
}
The server is configured to return a 400 - Bad Request
on http://localhost/test
along with a helpful message. A BadRequestException
is being thrown by ClientProxy
. Other than wrapping in try/catch
, how can I make getTest()
catch the exception and return the Response's helpful message as a string. I tried various ClientExceptionMapper
implementations, but just can seem to get it right. The above code doesn't ever call toException
. What am I missing here?
My current work-around is to use a ClientResponseFilter
and then do a setStatus(200)
and stuff the original status in the response entity. This way I avoid the exception throws.
RESTeasy is a Java library that provides a simple interface to the REST server. It supports all of the features of the Jakarta REST Services and includes support for both synchronous and asynchronous communication. Firstly, there are really two ways to create a REST Client. Use Jakarta REST Client API.
Approach 2 : Annotating a class with @ControllerAdvice and define methods with @ExceptionHandler. This is similar to Controller based exception (refer approach 1) but this is used when controller class is not handling the exception. This approach is good for global handling of exceptions in Rest Api.
ExceptionMapper is a contract for a provider that maps Java exceptions to Response object. An implementation of ExceptionMapper interface must be annotated with @Provider to work correctly.
Exception HandlerThe @ExceptionHandler is an annotation used to handle the specific exceptions and sending the custom responses to the client. Define a class that extends the RuntimeException class. You can define the @ExceptionHandler method to handle the exceptions as shown.
ClientExceptionMapper in Resteasy is deprecated (see java docs)
The JAX-RS 2.0 client proxy framework in resteasy-client module does not use org.jboss.resteasy.client.exception.mapper.ClientExceptionMapper.
Try with an ExceptionMapper like this:
import javax.ws.rs.ClientErrorException;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
@Provider
public class MyMapper implements ExceptionMapper<ClientErrorException> {
@Override
public Response toResponse(ClientErrorException e) {
return Response.fromResponse(e.getResponse()).entity(e.getMessage()).build();
}
}
Regards,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With