在 Debian 上写 Swagger(OpenAPI)文档,通常有三种常见方式:
下面按“从零开始”的思路说明。
sudo apt update
sudo apt install docker.io -y
sudo systemctl enable --now docker
docker run -d -p 8080:8080 swaggerapi/swagger-editor
访问:
http://localhost:8080
docker run -d -p 8081:8080 swaggerapi/swagger-ui
访问:
http://localhost:8081
swagger.yamlopenapi: 3.0.0
info:
title: User API
version: 1.0.0
description: Debian 上的 Swagger 示例
servers:
- url: http://localhost:8080
paths:
/users:
get:
summary: 获取用户列表
responses:
'200':
description: 成功
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/User'
/users/{id}:
get:
summary: 根据用户ID获取用户
parameters:
- name: id
in: path
required: true
schema:
type: integer
responses:
'200':
description: 成功
components:
schemas:
User:
type: object
properties:
id:
type: integer
name:
type: string
保存为 swagger.yaml,在 Swagger Editor 中打开即可。
pip install fastapi uvicorn
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class User(BaseModel):
id: int
name: str
@app.get("/users", response_model=list[User])
def get_users():
return [User(id=1, name="Tom")]
启动:
uvicorn main:app --reload
访问:
http://localhost:8000/docs
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.3.0</version>
</dependency>
访问:
http://localhost:8080/swagger-ui.html
go install github.com/swaggo/swag/cmd/swag@latest
swag init
sudo apt install nginx
server {
listen 80;
root /var/www/swagger;
index index.html;
}
把 Swagger UI 的 dist 目录放进去即可。
✅ Debian 写 Swagger 文档流程:
openapi.yaml如果你告诉我:
我可以直接给你一套完整示例。