Swagger是一種API文檔生成工具,它可以通過掃描API代碼或手動編寫描述文件來生成API文檔。Swagger UI是Swagger的一個交互式用戶界面,它可以讓開發者在瀏覽器中瀏覽和測試API。在CentOS系統上安裝和配置Swagger,可以使得API文檔的生成和展示更加方便和高效。以下是詳細介紹:
yum install gcc-c make -y
curl -sL https://rpm.nodesource.com/setup_12.x | bash -
yum install nodejs -y
mkdir /opt/swagger
cd /opt/swagger
wget https://github.com/swagger-api/swagger-editor/archive/v3.14.0.tar.gz
tar -xzf v3.14.0.tar.gz
cd swagger-editor-3.14.0
npm install -g http-server
http-server -p 8080
cd /opt/swagger
wget https://github.com/swagger-api/swagger-ui/archive/v3.34.0.tar.gz
tar -xzf v3.34.0.tar.gz
cd swagger-ui-3.34.0
npm install express --save
mkdir public
cp -r ../swagger-ui/dist/* public/
cd /opt/swagger
node index.js
訪問http://127.0.0.1:8000/static/index.html
,即可看到Swagger UI的界面。
git clone https://github.com/swagger-api/swagger-editor.git
cd swagger-editor
npm install -g http-server
http-server -p 8000
將生成的JSON文件放置在Swagger UI的public
目錄下。
修改public/index.html
文件中的URL,指向自己的API文檔文件。
重啟Swagger UI服務,即可在瀏覽器中查看和測試API文檔。
通過以上步驟,可以在CentOS系統上成功安裝和配置Swagger,從而生成和展示API文檔,提高API的可用性和易用性。