jersey - How to handle exceptions with Swagger? -
i building test apis using swagger (1.5) , jax-rs jersey (1.13) , m trying implement exception handling. example have following code when receiving results db (elasticsearch)
@post @path("/category") @apioperation(value="returns products") @produces({ "application/json" }) public response getpostcategories( @apiparam(value="keyphrase, required=true) @queryparam("keyphrase") string keyphrase, @apiparam(value="category) @queryparam("category") string category, @context securitycontext securitycontext) throws webapplicationexception { searchrequest searchrequest = new searchrequest(); searchrequest.setkeyphrase(keyphrase); searchrequest.setcategory(category); searchcategoryquery categoryquery = new searchcategoryquery(); string searchresponse = null; try { searchresponse = categoryquery.searchcategory(searchrequest); } catch (webapplicationexception ex) { throw new webapplicationexception(response.status(status.bad_request).entity("results no found").type(javax.ws.rs.core.mediatype.application_json).build()); } return response.ok(searchresponse).build(); }
however, in output swagger prints same response
what need instead receive error messages specify in each exception. ideas?
swagger not handle application exceptions yet.
you either need create custom exception classes (that extend java.lang.exception
) or use existing ones (like webapplicationexception
using) , make api definition throw these errors. need use java/j2ee/jersey throw proper exceptions. swagger ui display them you.
check this link details on rest exception handling.
Comments
Post a Comment