在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: A list of users
content:
application/json:
schema:
type: array
items:
$ref: '#/definitions/User'
definitions:
User:
type: object
properties:
id:
type: integer
name:
type: string
required:
- id
- name
node app.js
在瀏覽器中訪問 http://localhost:3000/api-docs,你應該能看到Swagger UI界面。
以上兩種方法可以幫助你在Ubuntu上集成Swagger UI,并查看和測試你的API文檔。