在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和文件路径可能需要根据你的实际环境进行调整。