在 Debian 系统中配置 Swagger,通常取决于你使用的后端语言/框架。下面我按最常见的几种场景给你说明,并给出在 Debian 上可直接操作的示例。如果你用的是其他语言,可以再告诉我。
cat /etc/os-release
确保已安装 Java / Node / Python 等(按你使用的技术栈):
sudo apt update
sudo apt install -y curl wget unzip
sudo apt install -y openjdk-17-jdk
java -version
pom.xml:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.3.0</version>
</dependency>
@Configuration
public class SwaggerConfig {
@Bean
public OpenAPI customOpenAPI() {
return new OpenAPI()
.info(new Info()
.title("API 文档")
.version("1.0")
.description("Debian 上的 Swagger 示例"));
}
}
mvn spring-boot:run
访问:
http://localhost:8080/swagger-ui.html
或(springdoc):
http://localhost:8080/swagger-ui/index.html
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs
mkdir swagger-demo && cd swagger-demo
npm init -y
npm install express swagger-ui-express swagger-jsdoc
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');
});
sudo apt install -y python3 python3-pip
pip3 install flask flask-swagger-ui
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
sudo ufw allow 8080
sudo ufw allow 3000
检查安全组 / iptables
你可以直接告诉我:
我可以给你完全针对你项目的 Swagger 配置方案。