java - Swagger UI causing HTTP 406 Not Acceptable response for operations producing content types other than json -
i have rest api published jersey , documented swagger, have swagger ui installation consuming api.
almost operations produce application/json , work expected, except 1 operation produces: 'text/plain;charset=utf-8'
when try call service swagger ui, server logs javax.ws.rs.notacceptableexception , returns 406 response. if call same service rest client works expected.
@get @path("/text") @produces(mediatype.text_plain + ";charset=utf-8") @apioperation(value= "return text") public response gettext(@queryparam("user") string user) { return response.ok(textservice.gettextforuser(user)).build(); }
if change @produces(mediatype.application_json + ";charset=utf-8") works fine, don't want set wrong content type.
the problem seems swagger ui wrongly setting accept headers application/json can seen observing request:
get /supertext/text?user=1 ... accept: application/json
when using rest client accept header are:
get /supertext/text?user=1 ... accept: */*
why swagger ui not setting accept headers properly?
can configured?
it seems swagger ui sets accept header application/json when finds @produces annotation contains single value, otherwise renders drop-down list in ui choose available content types.
in swagger-ui.js:
opts.responsecontenttype = $("div select[name=responsecontenttype]", $(this.el)).val();
when drop-down list doesn't exist, property becomes undefined.
later in code, response content type set application/json if property null or undefined:
in swagger.js:
if (this.type === "post" || this.type === "get" || this.type === "patch") { if (this.opts.responsecontenttype) { responsecontenttype = this.opts.responsecontenttype; } else { responsecontenttype = "application/json"; } }
so solution modify code in swagger-ui.js make sure correct content-type set, exploring produces array , choosing first element response content type:
in swagger-ui.js replace line:
opts.responsecontenttype = $("div select[name=responsecontenttype]", $(this.el)).val();
with:
if($("div select[name=responsecontenttype]", $(this.el)).val() === undefined) { opts.responsecontenttype = opts.parent.model.produces[0]; } else { opts.responsecontenttype = $("div select[name=responsecontenttype]", $(this.el)).val(); }
Comments
Post a Comment