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.
MyProxy.getTest()
should actually be throwing an exception which contains a helpful message. Then you would use an ExceptionMapper to map that exception to a 400 - Bad Request response (and you could include the message as the body of the response). – FGreg