在Linux上利用Swagger进行API调试,通常涉及安装Swagger UI和Swagger Editor,配置相关依赖,并将API文档暴露给调试界面。以下是详细的步骤:
安装Swagger UI和Swagger Editor
-
安装Node.js和npm:
sudo apt update sudo apt install -y nodejs npm -
下载并安装Swagger UI:
wget https://repo1.maven.org/maven2/io/springfox/springfox-swagger-ui/2.9.2/springfox-swagger-ui-2.9.2.html unzip springfox-swagger-ui-2.9.2.html sudo mv springfox-swagger-ui-2.9.2 /var/www/html/swagger-ui -
配置Swagger UI(可选):
编辑
/var/www/html/swagger-ui/index.html文件,找到以下行并进行修改:script window.onload function () { // Begin Swagger UI call region const ui = SwaggerUIBundle({ url: "http://petstore.swagger.io/v2/api-docs", dom_id: '#swagger-ui', deepLinking: true, presets: [ SwaggerUIBundle.presets.apis, SwaggerUIStandalonePreset ], plugins: [ SwaggerUIBundle.plugins.DownloadUrl ], layout: "StandaloneLayout" }); // End Swagger UI call region window.ui = ui; }; -
启动Web服务器(例如Apache或Nginx):
-
Apache:
sudo a2ensite default.conf sudo systemctl restart apache2 -
Nginx:
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/default.baksudo nano /etc/nginx/sites-available/default修改
server块中的root和index指令:server { listen 80 default_server; listen [::]: 80 default_server; root /var/www/html; index index.html index.htm; server_name _; location / { try_files $uri $uri/ /index.html; } }保存并退出编辑器,然后重启Nginx:
sudo systemctl restart nginx
-
-
安装并启动Swagger Editor:
npm install -g swagger-editor swagger editor http://localhost:9000现在,你应该能够通过浏览器访问
http://your_server_ip/swagger-ui和http://your_server_ip:9000来查看和使用 Swagger UI 和 Swagger Editor。
使用Swagger进行API调试
-
在Swagger中添加API调试和测试工具:
-
确保你已经安装了Swagger。如果还没有安装,可以使用以下命令进行安装:
npm install -g swagger -
在你的项目中创建一个名为
swagger.json的文件,用于描述你的API接口。 -
使用Swagger CLI生成API文档页面:
swagger project start my-api --host localhost --port 8080 --schemes http -
启动Swagger UI服务器:
swagger project start my-api -
打开浏览器,访问
http://localhost:8080,你将看到Swagger UI界面,其中包含了你的API接口信息。在这里,你可以进行API的调试和测试。
-
通过以上步骤,你可以在Linux上成功安装和配置Swagger,从而进行API调试。
以上就是关于“如何利用Swagger在Linux上进行API调试”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm