rest - Swagger Java common header -


i have rest api server implemented jersey2.0 , integrated swagger automate docs , client side applications.

we have our own authentication header (x-auth-token) should applied requests in order use them (beside login request retrieves token if credentials correct).

right need manually add the token apiimplicitparam annotation each of requests in order generated docs include it:

@apiimplicitparams({@apiimplicitparam(name = authfilter.authorization_property, value = "authorization token", required = true, datatype = "string", paramtype = "header")}) 

i don't want hardcode ui code add header itself, believe violated api encapsulation provided swagger.json. server definition should provide details necessary generated docs.

is there way define custom default annotation requests in swagger? or alternatively use kind of filter achieve it?

a quick @ annotations

the apiimplicitparam , apiimplicitparams annotations defined following:

@target(value=method) @retention(value=runtime) public @interface apiimplicitparam  @target(value=method) @retention(value=runtime) public @interface apiimplicitparams 

please note @target(value=method). means these annotations can applied methods.

for more details, check documentation.

what swagger ui can you

in swagger ui version 2.1.4 released on 6th january 2016 (the recent version when answer written), can have api key:

swagger ui page header api key

have @ index.html. default implementation sends api key query parameter:

function addapikeyauthorization(){     var key = encodeuricomponent($('#input_apikey')[0].value);     if(key && key.trim() != "") {         var apikeyauth = new swaggerclient.apikeyauthorization("api_key", key, "query");         window.swaggerui.api.clientauthorizations.add("api_key", apikeyauth);         log("added key " + key);     } } 

but can change send http header:

function addapikeyauthorization(){     var key = encodeuricomponent($('#input_apikey')[0].value);     if(key && key.trim() != "") {         swaggerui.api.clientauthorizations.add("key",              new swaggerclient.apikeyauthorization("authorization", key, "header"));         log("added authorization header " + key);     } } 

consider, example, api key you've entered in box credentials. when sending request, credentials value sent in authorization header (or in header you've defined):

request authorization header

for additional details, have documentation.

just quick hint

when testing pet store example, ensure header you've added matches values of header access-control-allow-headers. pet store example, allowed headers content-type, api_key , authorization.

hence, if try adding x-auth-token header in pet store example, you'll have following error:

xmlhttprequest cannot load http://petstore.swagger.io/v2/pet/findbystatus?status=active. request header field x-auth-token not allowed access-control-allow-headers in preflight response.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -