Class SwaggerUi

java.lang.Object
com.jsgalactic.axiom.openapi.ui.SwaggerUi

public final class SwaggerUi extends Object
Serves Swagger UI from the application itself, pointing at the application's own OpenAPI or Swagger document. Nothing is loaded from a CDN or any other origin: the UI files come from the org.webjars:swagger-ui artifact on the class path, are read once when the UI is registered and are served from memory.

 SwaggerUi.builder().spec("OpenAPI 3.1", "/openapi.json").build().register(app);   // GET /docs
 

Served routes (under the configured path, /docs by default):

  • GET /docs and GET /docs/: the HTML page. It has no inline script; it loads the files below from its own origin.
  • GET /docs/swagger-initializer.js: the generated start-up script that names your document URL(s).
  • GET /docs/assets/<ui version>/<file>: the bundled UI files, an exact allow-list (swagger-ui.css, swagger-ui-bundle.js, swagger-ui-standalone-preset.js, index.css, two favicons). Any other name, including anything that looks like a path traversal, answers 404; no file system or class path path is ever built from a request.
The page and the script are revalidated (Cache-Control: no-cache, strong ETag, 304 on a match); assets live under a versioned path and are immutable. Every response carries a strict Content-Security-Policy (scripts and connections only from the same origin, no framing), X-Content-Type-Options: nosniff and Referrer-Policy: no-referrer. The document URLs must therefore be same-origin paths such as /openapi.json. HEAD and OPTIONS are answered by the router like for any route.

The UI is a front end for your API description. Whether to expose it publicly is the application's choice: nothing is served until register(com.jsgalactic.axiom.application.Application, com.jsgalactic.axiom.context.Middleware...) is called, and it takes the same middleware as any route, so register the document routes and the UI with the same policy.

Instances are immutable and thread-safe.

  • Method Details

    • builder

      public static SwaggerUi.Builder builder()
      Starts a configuration.
      Returns:
      a builder
    • path

      public String path()
      Returns the path the UI is served under.
      Returns:
      the path, for example /docs
    • register

      public Route register(Application app, Middleware... middleware)
      Registers the UI routes. The UI files are read from the class path now, so a missing artifact stops startup instead of failing a request. Routes are hidden from generated documents.
      Parameters:
      app - application still configuring
      middleware - middleware for every UI route, for example an authentication policy
      Returns:
      the route serving the page
      Throws:
      IllegalStateException - if the Swagger UI artifact is not on the class path