在Ubuntu系统中调试Swagger接口,你可以遵循以下步骤:
sudo apt update
sudo apt install nodejs npm
接下来,使用npm全局安装Swagger UI:
sudo npm install -g swagger-ui-express
app.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 at http://localhost:${port}`);
});
swagger.yaml
的文件,并添加你的API接口定义。例如:swagger: '2.0'
info:
title: Sample API
description: A sample API to demonstrate Swagger UI.
version: '1.0.0'
host: localhost:3000
basePath: /api-docs
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
format: int64
name:
type: string
email:
type: string
format: email
node app.js
http://localhost:3000/api-docs
,你应该能看到Swagger UI界面,其中列出了你在swagger.yaml
文件中定义的所有API接口。你可以在这里测试和调试你的接口。注意:这里的示例使用了Express框架和YAML格式的Swagger文档。你也可以使用其他编程语言和文档格式,例如OpenAPI 3.0和JSON格式。只需相应地调整安装依赖项和代码即可。