在Ubuntu中使用Swagger UI有多种方法,以下是详细的步骤:
sudo snap refresh
sudo snap install swagger-ui
swagger-ui
这将在默认浏览器中打开Swagger UI界面。
sudo apt update
sudo apt install nodejs npm
sudo npm install -g swagger-ui-express
mkdir swagger-demo
cd swagger-demo
index.js 文件,并添加以下内容:const express = require('express');
const swaggerUi = require('swagger-ui-express');
const YAML = require('yamljs');
const app = express();
// 读取Swagger文档
const swaggerDocument = YAML.load('./swagger.yaml');
// 使用swagger-ui-express中间件
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
version: '1.0.0'
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
node index.js
http://localhost:3000/api-docs。sudo apt update
sudo apt install docker.io
docker pull swaggerapi/swagger-ui-express
docker run -p 8080:8080 -e SWAGGER_JSON=/app/swagger.json -v $(pwd):/app swaggerapi/swagger-ui-express
确保你的 swagger.json 文件位于当前目录中。
http://localhost:8080。通过以上方法,你可以在Ubuntu系统中成功安装并使用Swagger工具。选择适合你的方法进行操作即可。