下面给你一个Debian 上 Swagger 入门的实用路线,适合“刚接触 Swagger / OpenAPI”的情况。
Swagger 是一套 API 描述与可视化工具,现在标准叫 OpenAPI。
常见用途:
核心组件:
sudo apt update
sudo apt upgrade -y
sudo apt install -y curl wget git openjdk-17-jdk
sudo apt install -y docker.io
sudo systemctl enable --now docker
运行 Swagger UI:
docker run -p 8080:8080 swaggerapi/swagger-ui
浏览器访问:
http://localhost:8080
你会看到一个示例 API 文档。
sudo apt install -y nodejs npm
git clone https://github.com/swagger-api/swagger-ui.git
cd swagger-ui
npm install
npm run dev
访问:
http://localhost:3200
新建 swagger.yaml:
openapi: 3.0.0
info:
title: 示例 API
version: 1.0.0
paths:
/hello:
get:
summary: 返回问候
responses:
'200':
description: 成功
content:
application/json:
schema:
type: object
properties:
message:
type: string
在 Swagger UI 中:
swagger.yaml 路径或 URLpip install flask flask-restx
from flask import Flask
from flask_restx import Api, Resource
app = Flask(__name__)
api = Api(app, title="示例 API")
@api.route('/hello')
class Hello(Resource):
def get(self):
return {"message": "Hello Swagger"}
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
访问:
http://localhost:5000/
openjdk-17如果你愿意,可以告诉我:
我可以直接给你对应项目的 Swagger 示例。