温馨提示×

Debian中Swagger怎么配置

小樊
47
2026-08-25 14:39:00
栏目: 智能运维

在 Debian 系统中配置 Swagger,通常取决于你使用的后端语言/框架。下面我按最常见的几种场景给你说明,并给出在 Debian 上可直接操作的示例。如果你用的是其他语言,可以再告诉我。


一、先确认 Debian 环境

cat /etc/os-release

确保已安装 Java / Node / Python 等(按你使用的技术栈):

sudo apt update
sudo apt install -y curl wget unzip

二、常见场景 1:Java + Spring Boot 配置 Swagger

1️⃣ 安装 Java(如未安装)

sudo apt install -y openjdk-17-jdk
java -version

2️⃣ 在 Spring Boot 项目中引入 Swagger

✅ Spring Boot 2.x(Swagger 2)

pom.xml

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

✅ Spring Boot 3.x(推荐 Swagger 3 / springdoc-openapi

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
    <version>2.3.0</version>
</dependency>

3️⃣ 配置 Swagger

@Configuration
public class SwaggerConfig {

    @Bean
    public OpenAPI customOpenAPI() {
        return new OpenAPI()
                .info(new Info()
                        .title("API 文档")
                        .version("1.0")
                        .description("Debian 上的 Swagger 示例"));
    }
}

4️⃣ 启动项目并访问

mvn spring-boot:run

访问:

http://localhost:8080/swagger-ui.html

或(springdoc):

http://localhost:8080/swagger-ui/index.html

三、常见场景 2:Node.js + Express 配置 Swagger

1️⃣ 安装 Node.js(Debian)

curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs

2️⃣ 初始化项目

mkdir swagger-demo && cd swagger-demo
npm init -y

3️⃣ 安装依赖

npm install express swagger-ui-express swagger-jsdoc

4️⃣ 示例代码

const express = require('express');
const swaggerJsdoc = require('swagger-jsdoc');
const swaggerUi = require('swagger-ui-express');

const app = express();

const options = {
  definition: {
    openapi: '3.0.0',
    info: {
      title: 'Debian Swagger API',
      version: '1.0.0'
    }
  },
  apis: ['./index.js']
};

const spec = swaggerJsdoc(options);

app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(spec));

app.listen(3000, () => {
  console.log('http://localhost:3000/api-docs');
});

四、常见场景 3:Python + Flask 配置 Swagger

1️⃣ 安装 Python 环境

sudo apt install -y python3 python3-pip

2️⃣ 安装依赖

pip3 install flask flask-swagger-ui

3️⃣ 示例

from flask import Flask
from flask_swagger_ui import get_swaggerui_blueprint

app = Flask(__name__)

SWAGGER_URL = '/swagger'
API_URL = '/static/swagger.json'

swaggerui_blueprint = get_swaggerui_blueprint(
    SWAGGER_URL, API_URL,
    config={'app_name': "Debian Swagger"}
)

app.register_blueprint(swaggerui_blueprint, url_prefix=SWAGGER_URL)

@app.route('/static/swagger.json')
def swagger():
    return {
        "openapi": "3.0.0",
        "info": {"title": "Debian API", "version": "1.0"}
    }

app.run(host="0.0.0.0", port=5000)

访问:

http://localhost:5000/swagger

五、常见问题(Debian 特有)

❓ 端口无法访问

sudo ufw allow 8080
sudo ufw allow 3000

❓ 防火墙或云服务器

检查安全组 / iptables


六、我可以继续帮你

你可以直接告诉我:

  1. ✅ 使用的语言(Java / Node / Python / Go / PHP)
  2. ✅ 框架(Spring Boot / Express / Flask / FastAPI)
  3. ✅ Debian 版本(11 / 12)

我可以给你完全针对你项目的 Swagger 配置方案

0