1. 准备Linux环境基础依赖
在Linux系统上,首先需要安装Node.js和npm(Node.js包管理器),这是Swagger Editor和Swagger UI运行的基础环境。以Ubuntu/Debian为例,可通过以下命令安装:
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash - # 添加Node.js源
sudo apt-get install -y nodejs # 安装Node.js及npm
安装完成后,通过node -v和npm -v验证安装是否成功。
2. 安装并启动Swagger Editor(可选,用于编写/编辑API定义)
Swagger Editor是在线编写和验证OpenAPI规范的工具,支持实时语法检查和预览。
- 方式一:通过npm全局安装
sudo npm install -g swagger-editor # 全局安装Swagger Editor swagger-editor # 启动本地服务(默认端口8080) - 方式二:使用Docker容器化部署(推荐,便于管理)
启动后,通过浏览器访问docker pull swaggerapi/swagger-editor:v4.6.0 # 拉取最新版镜像 docker run -d -p 8080:8080 --name swagger-editor swaggerapi/swagger-editor:v4.6.0 # 运行容器http://<服务器IP>:8080即可使用Swagger Editor。
3. 安装并启动Swagger UI(用于可视化展示API文档)
Swagger UI是将OpenAPI规范(YAML/JSON)渲染为交互式文档的工具,支持“Try it out”等功能。
- 方式一:通过npm手动配置
创建sudo npm install -g swagger-ui-express express # 安装依赖 mkdir swagger-ui-project && cd swagger-ui-project # 创建项目目录index.js文件,配置静态文件服务和Swagger UI:启动服务:const express = require('express'); const swaggerUi = require('swagger-ui-express'); const swaggerDocument = require('./swagger.json'); // 引入OpenAPI定义文件 const app = express(); app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument)); // 映射访问路径 app.listen(3000, () => console.log('Swagger UI运行在 http://localhost:3000/api-docs'));node index.js。 - 方式二:使用Docker容器化部署
访问docker pull swaggerapi/swagger-ui:v4.15.5 # 拉取镜像 docker run -d -p 8081:8080 --name swagger-ui -e SWAGGER_FILE=/app/swagger.yaml -v /path/to/swagger.yaml:/app/swagger.yaml swaggerapi/swagger-ui:v4.15.5 # 挂载定义文件http://<服务器IP>:8081即可查看文档。
4. 编写OpenAPI规范文件(核心:定义API结构)
OpenAPI规范(YAML或JSON格式)是Swagger文档的基础,需描述API的路径、操作、参数、响应、模型等信息。示例如下(swagger.yaml):
swagger: '2.0'
info:
title: Linux API文档示例
version: 1.0.0
description: 用于演示Linux环境下的API文档共享
basePath: /api/v1
schemes:
- http
paths:
/users:
get:
summary: 获取用户列表
description: 返回所有用户的ID和名称
responses:
'200':
description: 成功获取用户列表
schema:
type: array
items:
$ref: '#/definitions/User'
definitions:
User:
type: object
properties:
id:
type: integer
example: 1
name:
type: string
example: 张三
将上述文件保存为swagger.yaml(或swagger.json),放置在项目根目录或指定路径。
5. 集成Swagger到应用(可选,自动生成文档)
若使用Spring Boot框架,可通过springdoc-openapi依赖自动生成OpenAPI规范,无需手动编写:
- 添加Maven依赖:
<dependency> <groupId>org.springdocgroupId> <artifactId>springdoc-openapi-starter-webmvc-uiartifactId> <version>2.1.0version> dependency> - 配置应用属性(
application.properties):springdoc.api-docs.path=/api-docs # OpenAPI规范路径 springdoc.swagger-ui.path=/swagger-ui # Swagger UI访问路径 - 启动应用后,访问
http://<服务器IP>:8080/swagger-ui即可查看自动生成的文档。
6. 共享API文档的方法
- 直接通过公网IP访问:若服务器有公网IP,可直接通过
http://<公网IP>:8080(Swagger Editor)或http://<公网IP>:8081/api-docs(Swagger UI)访问。 - 内网穿透(无公网IP):使用Cpolar、ngrok等工具将本地服务映射到公网。例如,使用Cpolar配置Swagger Editor公网地址:
根据Cpolar返回的公网地址(如curl -L https://www.cpolar.com/static/downloads/install-release-cpolar.sh | sudo bash # 安装Cpolar sudo systemctl start cpolar # 启动服务 cpolar http 8080 # 映射Swagger Editor的8080端口https://xxx.cpalr.com)访问文档。 - 权限控制(安全):为避免未授权访问,可通过以下方式增强安全性:
- Docker容器权限:限制容器端口映射范围(如仅允许内网IP访问)。
- Web服务器配置:使用Nginx/Apache添加Basic Auth认证,示例Nginx配置:
通过server { listen 80; server_name your-domain.com; location /api-docs { auth_basic "Restricted Access"; auth_basic_user_file /etc/nginx/.htpasswd; # 存放用户名密码的文件 proxy_pass http://localhost:3000; # 代理到Swagger UI服务 } }htpasswd命令创建密码文件:htpasswd -c /etc/nginx/.htpasswd admin。
通过以上步骤,即可在Linux环境下完成Swagger的部署与配置,实现API文档的共享与协作。
以上就是关于“如何在Linux环境下利用Swagger进行API文档共享”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm