Jersey Swagger

Adding the dependencies to your application


Hooking up Swagger-Core in your Application

Using a custom Application subclass

public class JerseyApplication extends ResourceConfig {
	public JerseyApplication() {
		packages("", "");

Configure and Initialize Swagger

Using Swagger’s Servlet in the web.xml


A few things to note:

  1. The api.version should reflect the version of your own API.
  2. swagger.api.basepath should point to the context root of your API. This defers from server to server and how you configured your JAX-RS application.
  3. There’s no <servlet-mapping> for this servlet as it is only used for initialization and doesn’t actually expose any interface.

Swagger UI

Swagger UI 2.x

  1. Download
  2. cp -avx swagger-ui-2.x/dist/ webapps/
  3. vi index.html:
    • -> http://localhost:8080/rest/jersey/swagger.json
    • -> /api

Swagger UI 3.x

Quick Annotation Overview

Name Description
@Api Marks a class as a Swagger resource.
@ApiImplicitParam Represents a single parameter in an API Operation.
@ApiImplicitParams A wrapper to allow a list of multiple ApiImplicitParam objects.
@ApiModel Provides additional information about Swagger models.
@ApiModelProperty Adds and manipulates data of a model property.
@ApiOperation Describes an operation or typically a HTTP method against a specific path.
@ApiParam Adds additional meta-data for operation parameters.
@ApiResponse Describes a possible response of an operation.
@ApiResponses A wrapper to allow a list of multiple ApiResponse objects.
@Authorization Declares an authorization scheme to be used on a resource or an operation.
@AuthorizationScope Describes an OAuth2 authorization scope.
@ResponseHeader Represents a header that can be provided as part of the response.

The latest release also adds a number of annotations for adding extensions and metadata at the Swagger Definition level:

Name Description
@SwaggerDefinition Definition-level properties to be added to the generated Swagger definition
@Info General metadata for a Swagger definition
@Contact Properties to describe the contact person for a Swagger definition
@License Properties to describe the license for a Swagger definition
@Extension Adds an extension with contained properties
@ExtensionProperty Adds custom properties to an extension


Setting scan.all.resources


CORS Support

  • ApiOriginFilter


Loading a Swagger spec (.json/.yaml) that is protected by Basic auth

vi index.html

const url = "http://localhost:8080/rest/jersey/swagger.json";
const ui = SwaggerUIBundle({
  url: url,
  requestInterceptor: (req) => {
    if (req.url === url) {
      req.headers.Authorization = "Basic " + btoa("t5750" + ":" + "123");
    return req;

Auto-add the Authorization header to all “try it out” requests

Swagger UI 2.x: vi index.html

swaggerUi.api.clientAuthorizations.add("key", new SwaggerClient.ApiKeyAuthorization("Authorization", "Basic dDU3NTA6MTIz", "header"));

Swagger UI 3.x: vi index.html

requestInterceptor: (req) => {
  if (!req.loadSpec) {
    req.headers.Authorization = 'Basic ' + btoa('t5750:123');
  return req;

Or vi index.html

onComplete: function() {
  // "basicAuth" is the key name of the security scheme in securityDefinitions
  // ui.preauthorizeBasic("basicAuth", "username", "password");
  ui.preauthorizeApiKey("Authorization", 'Basic ' + btoa('t5750:123'));

Or JerseyApplication

Swagger swagger = new Swagger().info(info);
		new ApiKeyAuthDefinition(Globals.AUTHORIZATION, In.HEADER));
SecurityRequirement securityRequirement = new SecurityRequirement();
new SwaggerContextService().updateSwagger(swagger);