Java P Java P - 2 months ago 64
reST (reStructuredText) Question

Swagger UI passing authentication token to API call in header

I am new to Swagger.

I am using Swagger UI to generate swagger documentation. I have two API calls. First call is to generate token based on user name and password. Second call needs token generated by first call.

How I set that token for second call using Swagger UI?

Answer

@ApiImplicitParams and @ApiImplicitParam will do the trick:

@GET
@Produces("application/json")
@ApiImplicitParams({
    @ApiImplicitParam(name = "Authorization", value = "Authorization token", 
                     required = true, dataType = "string", paramType = "header")
})
public String getUser(@PathParam("username") String userName) {
    ...
}

Read more here.

The Swagger UI will be updated so you can send your token from there. No changes to HTML will be necessary.


Important: A while ago, when documenting a REST API with Swagger, I realized that just adding @ApiImplicitParam is not enough (even if you have only one parameter). Anyway, you must add @ApiImplicitParams too.