首页主机资讯怎样在CentOS中集成Swagger UI

怎样在CentOS中集成Swagger UI

时间2025-12-08 17:51:04发布访客分类主机资讯浏览579
导读:在CentOS中集成Swagger UI,可以按照以下步骤进行: 1. 安装必要的软件包 首先,确保你的CentOS系统已经安装了必要的软件包,包括Java、Maven和Node.js。 sudo yum install java-1.8....

在CentOS中集成Swagger UI,可以按照以下步骤进行:

1. 安装必要的软件包

首先,确保你的CentOS系统已经安装了必要的软件包,包括Java、Maven和Node.js。

sudo yum install java-1.8.0-openjdk-devel maven nodejs npm

2. 下载Swagger UI

你可以从Swagger官方网站下载Swagger UI的源码,或者使用npm安装。

方法一:下载源码

wget https://github.com/swagger-api/swagger-ui/archive/master.zip
unzip master.zip
cd swagger-ui-master

方法二:使用npm安装

npm install -g swagger-ui-express

3. 配置Swagger UI

如果你下载了源码,需要配置Swagger UI以指向你的API文档。

方法一:手动配置

编辑swagger-ui-master/index.html文件,找到以下行:

<
    script>

  window.onload = function() {

    // Begin Swagger UI call region
    const ui = SwaggerUIBundle({

      url: "YOUR_API_SPEC_URL",
      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>
    

YOUR_API_SPEC_URL替换为你的API文档的URL。

方法二:使用swagger-ui-express

如果你使用的是swagger-ui-express,可以创建一个简单的Express应用来集成Swagger UI。

const express = require('express');
    
const swaggerUi = require('swagger-ui-express');
    
const YAML = require('yamljs');
    

const app = express();
    

// Load API definition from YAML file
const swaggerDocument = YAML.load('./path/to/your/swagger.yaml');
    

// Serve Swagger docs
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
    

// Start the server
const port = process.env.PORT || 3000;
    
app.listen(port, () =>
 {

  console.log(`Server is running on port ${
port}
    `);

}
    );
    

4. 启动应用

如果你使用的是手动配置的方法,可以直接在浏览器中打开swagger-ui-master/index.html文件。

如果你使用的是Express应用,启动服务器:

node your-app-file.js

然后在浏览器中访问http://your-server-ip:3000/api-docs即可看到Swagger UI界面。

5. 配置防火墙

确保你的防火墙允许访问相应的端口(例如3000)。

sudo firewall-cmd --zone=public --add-port=3000/tcp --permanent
sudo firewall-cmd --reload

通过以上步骤,你就可以在CentOS系统中成功集成Swagger UI了。

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: 怎样在CentOS中集成Swagger UI
本文地址: https://pptw.com/jishu/766141.html
CentOS下Swagger与Spring Boot如何联调 CentOS上Swagger API文档生成步骤

游客 回复需填写必要信息