java - What to use in failures and error case: WebApplicationException vs Response? -
i working on rest service , confuse in case of failures/errors should use response
or webapplicationexception
?
below example using response return 400 bad request error message along json response went wrong.
@get @path("/data") @produces(mediatype.application_json) public response getdata(@context uriinfo uriinfo) { string client = getclientid(); if (client == null || client.isempty()) { return response.status(httpservletresponse.sc_bad_request) .entity("{\"error\": \"client id null or empty\"}").build(); } // code , return succesfull response return response .status(httpservletresponse.sc_ok) .entity(resp.getresponse()).build(); }
what right thing here? if need use webapplicationexception
how can use return 400 bad request error message along json response
in particular case, whether to return response
or throw mapped exception, matter of preference. there cases returning response
not possible, instance if tied interface contract, return type not response
, or not in context of resource method, in filter or interceptor.
if need throw exception , not possible return response
(or you'd rather throw exception), if @ constructors of webapplicationexception
, you'll notice has constructor excepts response
. that's way add body if throwing exception (unless use mapper).
Comments
Post a Comment