下面以 Debian 系统 为背景,说明 Swagger 如何接入项目。Swagger 本身是一套规范,常见实现是 OpenAPI + Swagger UI / Swagger Editor,不同语言接入方式不同。我按常见场景给你讲。
在 Debian 上常见情况:
你可以对号入座。
适合:
swagger.json / openapi.yaml)sudo apt update
sudo apt install nginx -y
cd /var/www
sudo git clone https://github.com/swagger-api/swagger-ui.git
server {
listen 80;
server_name api.example.com;
location / {
root /var/www/swagger-ui/dist;
index index.html;
}
}
编辑:
/var/www/swagger-ui/dist/index.html
找到:
url: "https://petstore.swagger.io/v2/swagger.json"
改成你的:
url: "/swagger.json"
然后访问:
http://服务器IP
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.3.0</version>
</dependency>
http://localhost:8080/swagger-ui.html
java -jar your-app.jar
npm install swagger-ui-express
const swaggerUi = require('swagger-ui-express');
const swaggerDocument = require('./swagger.json');
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
访问:
http://localhost:3000/api-docs
from fastapi import FastAPI
from fastapi.openapi.docs import get_swagger_ui_html
app = FastAPI()
@app.get("/docs", include_in_schema=False)
async def custom_swagger_ui():
return get_swagger_ui_html("/openapi.json", "API Docs")
默认已自带:
/docs
/var/www 用 www-data为了给你更精准的步骤,你可以说:
我可以直接给你 可复制的 Debian 配置。