java - RESTEasy Client Exception Handling -
i have simple client using resteasy 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 bad request exception"); return null; } } the server configured return 400 - bad request on http://localhost/test along helpful message. badrequestexception being thrown clientproxy. other wrapping in try/catch, how can make gettest() catch exception , return response's helpful message string. tried various clientexceptionmapper implementations, can seem right. above code doesn't ever call toexception. missing here?
my current work-around use clientresponsefilter , setstatus(200) , stuff original status in response entity. way avoid exception throws.
i'd advise going through jax-rs client api unless there's functionality need using resteasy clients. (resteasy ships jax-rs, there's no library differences)
client client = clientfactory.newclient(); webtarget target = client.target("http://localhost/test"); response response = target.request().get(); if ( response.getstatuscode() != response.status.ok.getstatuscode() ) { system.out.println( response.readentity(string.class) ); return null; } string value = response.readentity(string.class); response.close(); the reason why mapper not work because client not throwing exception. client returning valid result proxy, , proxy reading that, , throwing exception, happens after mapper can intercept it.
Comments
Post a Comment