在Ubuntu上集成Swagger UI可以通過多種方法實現,以下是幾種常見的方法:
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'
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
format: int64
username:
type: string
firstName:
type: string
lastName:
type: string
email:
type: string
format: email
password:
type: string
format: password
phoneNumber:
type: string
format: phone
active:
type: boolean
default: true
dateCreated:
type: string
format: date-time
updated:
type: string
format: date-time
id:
$ref: '#/definitions/User'
node app.js
http://localhost:3000/api-docs,你應該能看到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');
// Load Swagger document
const swaggerDocument = YAML.load('./swagger.yaml');
const app = express();
// Use swagger-ui-express middleware
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: This is a sample API documentation for Swagger.
version: 1.0.0
host: localhost:8080
basePath: /api
schemes:
- http
paths:
/users:
get:
summary: Get list of users
description: Returns a list of all users
produces:
- application/json
responses:
'200':
description: A list of users
schema:
type: array
items:
$ref: '#/definitions/User'
definitions:
User:
type: object
properties:
id:
type: integer
format: int64
username:
type: string
firstName:
type: string
lastName:
type: string
email:
type: string
format: email
password:
type: string
format: password
phoneNumber:
type: string
format: phone
active:
type: boolean
default: true
dateCreated:
type: string
format: date-time
updated:
type: string
format: date-time
id:
$ref: '#/definitions/User'
node index.js
http://localhost:3000/api-docs。通過以上幾種方法,你可以在Ubuntu上成功集成Swagger UI,并查看和測試你的API文檔。選擇適合你的方法并按照步驟操作即可。