温馨提示×

怎样在Debian配Swagger

小樊
44
2026-09-28 20:35:17
栏目: 智能运维

在 Debian 上“配 Swagger”通常有几种常见场景,我先按最常见的情况说明,你再告诉我你具体用的是哪种技术栈,我可以给更精确的命令。


一、常见场景说明(先对号入座)

  1. Java / Spring Boot 项目
    • 用 Springdoc OpenAPI(推荐,替代旧 Swagger2)
  2. Node.js / Express
    • 用 swagger-ui-express + openapi
  3. 只是本地查看 Swagger UI(不写代码)
    • 直接装 swagger-ui 静态页面
  4. Python(FastAPI / Flask)
    • FastAPI 自带 Swagger

下面分别说。


二、Debian 基础准备(通用)

sudo apt update
sudo apt install -y curl wget gnupg

三、Spring Boot(最常用)

1️⃣ 添加依赖(Maven)

<dependency>
  <groupId>org.springdoc</groupId>
  <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
  <version>2.5.0</version>
</dependency>

2️⃣ 访问 Swagger UI

启动后浏览器打开:

http://localhost:8080/swagger-ui.html

✅ 不需要额外配置


四、Node.js + Express

1️⃣ 安装

sudo apt install -y nodejs npm
npm init -y
npm install express swagger-ui-express yamljs

2️⃣ 示例代码

const express = require('express');
const swaggerUi = require('swagger-ui-express');
const YAML = require('yamljs');

const swaggerDocument = YAML.load('./swagger.yaml');
const app = express();

app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));

app.listen(3000);

访问:

http://localhost:3000/api-docs

五、纯静态 Swagger UI(不写后端)

sudo apt install -y nginx
sudo mkdir -p /var/www/swagger
cd /var/www/swagger
wget https://github.com/swagger-api/swagger-ui/archive/refs/tags/v5.17.14.zip
unzip v5.17.14.zip

配置 Nginx:

server {
    listen 80;
    root /var/www/swagger/swagger-ui-5.17.14/dist;
    index index.html;
}

六、FastAPI(Python)

pip install fastapi uvicorn

自动可用:

http://localhost:8000/docs

七、我需要知道的信息(可选)

你可以告诉我:

  • ✅ 用的什么语言 / 框架?
  • ✅ 是开发环境还是服务器部署?
  • ✅ 想用 Swagger 2 还是 OpenAPI 3?

我可以直接给你可复制的完整配置。

0 踩