java ee - Jersey RS: Include object/JSON in void GET method? -
i have bailiwick of jersey rest service , have method returns void on success returns errorresponse object if there's issue. here's method looks like:
@get @path(resourceconstants.path_start_batch) @produces(mediatype.application_json) public void startbatch(@queryparam(resourceconstants.param_batch_id) string batchid) { seek { batchservice.startbatch(batchid); } grab (exception ex) { errorresponse errorresponse = new errorresponse(); map<string, string> responsedetails = errorresponse.getresponsedetails(); responsedetails.put("failure", "failed start exception " + ex.getmessage()); throw webserviceexception.generate(ex, errorresponse); } } and webserviceexception class looks this:
public class webserviceexception extends webapplicationexception { private static final long serialversionuid = 1l; public webserviceexception(errorresponse errorresponse) { super(response.servererror() .status(status.internal_server_error) .entity(errorresponse) .type(mediatype.application_json) .build()); } public webserviceexception(throwable throwable, response response) { super(throwable, response); } public static webserviceexception generate(throwable throwable, errorresponse errorresponse) { throwable rootcause = webserviceexception.getrootcause(throwable); response response = response.servererror() .status(status.internal_server_error) .entity(errorresponse) .type(mediatype.application_json) .build(); homecoming new webserviceexception(rootcause, response); } private static throwable getrootcause(throwable current) { throwable root = current.getcause(); if (root != null) { homecoming webserviceexception.getrootcause(root); } else { homecoming current; } } } when test invalid batch id, response empty set of empty braces {}.
can homecoming json object in error if method returns void?
do that:
@get @path(resourceconstants.path_start_batch) @produces(mediatype.application_json) public response startbatch(@queryparam(resourceconstants.param_batch_id) string batchid) { seek { //you code homecoming response.status(status.no_content).build(); //or response.ok(); } grab (runtimeexception ex) { errorresponse errorresponse = new errorresponse(); map<string, string> responsedetails = errorresponse.getresponsedetails(); responsedetails.put("failure", "failed start exception " + ex.getmessage()); homecoming response.status(status.internal_server_error).entity(errorresponse).build(); } } in general, not suggest homecoming empty response on request, improve utilize post.
java-ee jersey-2.0
No comments:
Post a Comment