在Linux下配置Swagger主要包括安裝Swagger UI和Swagger Editor,以及配置Web服務器。以下是詳細的步驟:
Swagger需要Java運行環境(JRE)或Java開發工具包(JDK)。你可以通過以下命令安裝OpenJDK:
sudo apt update
sudo apt install openjdk-11-jdk
驗證安裝:
java -version
Swagger使用Maven進行構建和依賴管理。你可以通過以下命令安裝Maven:
sudo apt install maven
驗證安裝:
mvn -version
你可以從Swagger的官方GitHub倉庫克隆Swagger Core項目:
git clone https://github.com/swagger-api/swagger-core.git
cd swagger-core
使用Maven構建Swagger Core項目:
mvn clean install
wget https://repo1.maven.org/maven2/io/springfox/springfox-swagger-ui/2.9.2/springfox-swagger-ui-2.9.2.zip
unzip springfox-swagger-ui-2.9.2.zip
/var/www/html
):sudo mv springfox-swagger-ui-2.9.2 /var/www/html/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;
};
</script>
sudo nano /etc/apache2/sites-available/swagger.conf
添加以下內容:
<VirtualHost *:80>
ServerName localhost
DocumentRoot /var/www/html
Directory /var/www/html
Options Indexes FollowSymLinks AllowOverride All Require all granted
</VirtualHost>
sudo a2ensite swagger.conf
sudo systemctl reload apache2
sudo nano /etc/nginx/sites-available/swagger
添加以下內容:
server {
listen 80;
server_name localhost;
root /var/www/html;
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html;
}
}
sudo ln -s /etc/nginx/sites-available/swagger /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
根據你使用的Web服務器(Apache或Nginx),啟動相應的服務:
sudo systemctl restart apache2
sudo systemctl restart nginx
現在,你應該能夠通過瀏覽器訪問以下URL來查看和使用Swagger UI:
請注意,上述步驟中的URL和文件路徑可能需要根據你的實際環境進行調整。