在 Linux 环境下生成 Swagger 文档,常见做法取决于你使用的后端语言/框架。下面按主流场景给你一个通用 + 示例说明,基本都能在 Linux 上直接操作。
无论是哪种语言,核心流程通常是:
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.2.0</version>
</dependency>
mvn spring-boot:run
http://localhost:8080/swagger-ui.html
http://localhost:8080/v3/api-docs
✅ 无需手动生成,Spring Boot 自动生成
go install github.com/swaggo/swag/cmd/swag@latest
// @Summary 获取用户
// @Description 获取用户信息
// @Tags user
// @Produce json
// @Success 200 {string} string "ok"
// @Router /user [get]
swag init
生成文件:
docs/
├── swagger.json
├── swagger.yaml
└── docs.go
http://localhost:8080/swagger/index.html
FastAPI 自动生成 Swagger
pip install fastapi uvicorn
from fastapi import FastAPI
app = FastAPI()
@app.get("/items/{item_id}")
def read_item(item_id: int):
return {"item_id": item_id}
启动:
uvicorn main:app --host 0.0.0.0 --port 8000
访问:
http://localhost:8000/docs
✅ 无需任何额外配置
npm install swagger-ui-express swagger-jsdoc
const swaggerJsdoc = require('swagger-jsdoc');
const swaggerUi = require('swagger-ui-express');
const options = {
definition: {
openapi: '3.0.0',
info: { title: 'API', version: '1.0.0' }
},
apis: ['./index.js']
};
const swaggerSpec = swaggerJsdoc(options);
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));
访问:
http://localhost:3000/api-docs
如果你已有接口但没写注解:
swagger-inline api.log
| 场景 | 命令 |
|---|---|
| Go | swag init |
| Java | 自动生成 |
| Python | 自动生成 |
| Node | 代码生成 |
| 导出 JSON | curl http://localhost/v3/api-docs > swagger.json |
你可以直接告诉我:
我可以给你一步一步的 Linux 实操命令。