Swagger UI 与文档发布

把规范渲染成可交互文档:静态托管、Docker、服务内嵌三种方式。

Intermediate
Key PointsSwagger UI 是一个纯前端页面,只要给它一个规范 URL 就能渲染;发布方式取决于你想让谁访问。

三种发布方式

WayWho can reach itBest for
In-app endpointAnyone who can reach the serviceInternal teams
Static hostingPublicOpen API docs
Docker / self-hostWhatever you exposeAir-gapped / on-prem

服务内嵌(Spring Boot)

With springdoc, /swagger-ui.html is the UI and /v3/api-docs is the spec itself. In production you usually disable the UI and keep only the spec for gateways or internal tooling.

YAML
# application.yml
springdoc:
  api-docs:
    path: /v3/api-docs
  swagger-ui:
    path: /swagger-ui.html
    enabled: false        # 生产关掉 UI

静态托管

Put openapi.yaml and the Swagger UI dist on any static server; the page only needs a url pointing at the spec.

Bash
npm i swagger-ui-dist
cp -r node_modules/swagger-ui-dist/* public/docs/
cp openapi.yaml public/docs/

# public/docs/index.html 里改成:
#   url: "./openapi.yaml"

Docker 自托管

Bash
docker run -d --name swagger-ui -p 8080:8080 \
  -e URL=https://api.example.com/v1/openapi.yaml \
  swaggerapi/swagger-ui
Before publishing, strip internal hostnames, test accounts and internal field names. Whatever sits in servers — including http://10.x.x.x — shows up in the docs and the try-it console.

用 Redoc 做只读文档

Redoc uses a three-column layout (nav, body, samples) and reads better for external audiences; Swagger UI is better for hands-on testing. Both read the same spec.

HTML
<script src="https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js"></script>
<redoc spec-url="./openapi.yaml"></redoc>

Friends & Partners