在Ubuntu中部署Swagger(通常指OpenAPI)可以通过多种方法实现,以下是两种常见的方法:使用Docker和使用Node.js和Express。以下是详细的步骤:
sudo apt update sudo apt install docker.io
docker pull swaggerapi/swagger-ui-express
docker run -p 8080:8080 swaggerapi/swagger-ui-express
http://localhost:8080
,你应该能看到Swagger UI界面。sudo apt update sudo apt install nodejs npm
mkdir swagger-ui-project cd swagger-ui-project
npm init -y
npm install swagger-ui-express
app.js
的文件,并添加以下代码:const express = require('express'); const swaggerUi = require('swagger-ui-express'); const YAML = require('yamljs'); // Load Swagger document const swaggerDocument = YAML.load('./swagger.yaml'); const app = express(); // Serve Swagger docs app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument)); const PORT = process.env.PORT || 3000; app.listen(PORT, () => { console.log(`Server is running on port ${PORT}`); });
swagger.yaml
的文件,并添加你的API文档。例如:swagger: '2.0' info: title: Sample API description: A sample API to demonstrate Swagger UI integration version: '1.0.0' host: localhost:3000 basePath: /api schemes: - http paths: /users: get: summary: List all users responses: '200': description: An array of users schema: type: array items: ref: '#/definitions/User' definitions: User: type: object properties: id: type: integer name: type: string email: type: string
node app.js
http://localhost:3000/api-docs
,你应该能看到Swagger UI界面。通过以上两种方法,你可以在Ubuntu上集成Swagger UI,并查看和测试你的API文档。