在Ubuntu上自定义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
在项目文件夹中运行以下命令来启动应用:
node app.js
在浏览器中访问 http://localhost:3000/api-docs
,你应该能看到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界面。
knife4j-openapi3-ui
。/src/style/knife4j.less
中。你可以通过修改这个文件来自定义Swagger UI的主题。如果你使用的是 knife4j-vue
项目,那么你需要使用 npm run build
进行打包,生成 dist
文件夹并替换依赖。docs.html
文件,解析JSON数据并渲染自定义UI界面。swagger-ui-layer
(基于Vue的项目)允许通过修改源码实现样式自定义。通过以上方法,你可以在Ubuntu上自定义Swagger界面,提升接口文档的可读性和美观度。