linux

linux swagger怎么安装

小樊
81
2024-12-28 19:32:14
栏目: 智能运维

在 Linux 上安装 Swagger,通常是指安装 Swagger UI 和 Swagger Editor。以下是安装步骤:

安装 Swagger UI

  1. 更新包列表

    sudo apt update
    
  2. 安装必要的依赖

    sudo apt install -y openjdk-11-jre-headless
    
  3. 下载 Swagger UI

    wget https://repo1.maven.org/maven2/io/springfox/springfox-swagger-ui/2.9.2/springfox-swagger-ui-2.9.2.html
    
  4. 解压 Swagger UI

    unzip springfox-swagger-ui-2.9.2.html
    
  5. 移动解压后的文件到 Web 服务目录(例如 /var/www/html):

    sudo mv springfox-swagger-ui-2.9.2 /var/www/html/swagger-ui
    
  6. 配置 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>
    
  7. 启动 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.bak
      sudo nano /etc/nginx/sites-available/default
      
      修改 server 块中的 rootindex 指令:
      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/ =404;
          }
      }
      
      保存并退出编辑器,然后重启 Nginx:
      sudo systemctl restart nginx
      

安装 Swagger Editor

  1. 下载 Swagger Editor

    wget https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/3.50.0/swagger-editor.min.js
    wget https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/3.50.0/swagger-editor.min.css
    
  2. 移动文件到 Web 服务目录(例如 /var/www/html):

    sudo mv swagger-editor.min.js /var/www/html/swagger-editor.js
    sudo mv swagger-editor.min.css /var/www/html/swagger-editor.css
    
  3. 创建一个简单的 HTML 文件来加载 Swagger Editor

    sudo nano /var/www/html/swagger-editor.html
    

    添加以下内容:

    <!DOCTYPE html>
    <html>
    <head>
      <link rel="stylesheet" type="text/css" href="swagger-editor.css">
    </head>
    <body>
      <div id="swagger-editor"></div>
      <script src="swagger-editor.js"></script>
      <script>
        window.onload = function() {
          const editor = SwaggerEditor({
            url: "https://petstore.swagger.io/v2/api-docs",
            dom_id: '#swagger-editor'
          });
        };
      </script>
    </body>
    </html>
    
  4. 启动 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.bak
      sudo nano /etc/nginx/sites-available/default
      
      修改 server 块中的 rootindex 指令:
      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/ =404;
          }
      }
      
      保存并退出编辑器,然后重启 Nginx:
      sudo systemctl restart nginx
      

现在,你应该能够通过浏览器访问 http://your_server_ip/swagger-uihttp://your_server_ip/swagger-editor 来查看和使用 Swagger UI 和 Swagger Editor。

0
看了该问题的人还看了