温馨提示×

debian swagger有教程吗

小樊
49
2026-09-07 18:20:06
栏目: 智能运维

有的,下面给你几个在 Debian 上学习和使用 Swagger(OpenAPI)的常见路线和教程思路,从「看文档」到「实际上手」都覆盖。


一、Swagger 是什么(先明确概念)

Swagger 现在一般指 OpenAPI 规范 + Swagger 工具链:

  • Swagger UI:把 API 文档可视化
  • Swagger Editor:写 OpenAPI 文件
  • Swagger Codegen / OpenAPI Generator:生成客户端/服务端代码

二、Debian 上常见使用方式(推荐)

方式 1:直接用 Swagger UI(最简单)

适合:只看 API 文档

sudo apt update
sudo apt install docker.io
docker run -p 8080:8080 swaggerapi/swagger-ui

浏览器打开:

http://localhost:8080

✅ 不需要写代码,适合快速看文档


方式 2:Node.js + Swagger(开发 API 常用)

适合:写后端 API

1. 安装 Node.js(Debian)

curl -fsSL https://deb.nodesource.com/setup_20.x | sudo bash -
sudo apt install -y nodejs

2. 初始化项目

mkdir swagger-demo && cd swagger-demo
npm init -y
npm install express swagger-ui-express yamljs

3. 示例代码

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, () => {
  console.log('Swagger UI: http://localhost:3000/api-docs');
});

4. 访问

http://localhost:3000/api-docs

方式 3:Python(FastAPI 自带 Swagger)

如果你用 Python,强烈推荐 FastAPI

pip install fastapi uvicorn
from fastapi import FastAPI

app = FastAPI()

@app.get("/items")
def read_items():
    return [{"name": "item1"}]

运行:

uvicorn main:app --reload

自动文档:

http://127.0.0.1:8000/docs

✅ 零配置 Swagger


三、官方 & 中文教程推荐

  • 官方文档(英文):https://swagger.io/docs/
  • OpenAPI 规范:https://spec.openapis.org/
  • Swagger Editor 在线:https://editor.swagger.io/
  • 中文社区教程(搜索):「Swagger UI Debian 部署」

四、你可以告诉我:

  • 你是 前端 / 后端 / 运维?
  • 想用 哪种语言(Node / Python / Java)?
  • 是想 看文档 还是 自动生成 API?

我可以直接给你一份 Debian 上的完整实战教程。

0 踩