Swagger在Debian上的集成方法主要分为两类:基于Node.js(适用于JavaScript/Node.js应用)和基于Java(适用于Spring Boot应用)。以下是详细步骤:
更新系统包列表并安装Node.js、npm(Node.js包管理器):
sudo apt update && sudo apt upgrade -y
sudo apt install nodejs npm -y
# 验证安装
node -v && npm -v # 应输出版本号(如v18.x.x、9.x.x)
使用npm全局安装swagger-ui-express(用于集成Swagger UI到Express应用)和swagger-jsdoc(用于从代码注释生成Swagger文档):
sudo npm install -g swagger-ui-express swagger-jsdoc
在项目根目录下创建swagger.json(或swagger.yaml),定义API规范。示例如下:
{
"swagger": "2.0",
"info": {
"title": "Sample API",
"description": "A sample API to demonstrate Swagger integration",
"version": "1.0.0"
},
"basePath": "/api",
"paths": {
"/users": {
"get": {
"summary": "List all users",
"responses": {
"200": {
"description": "A list of users"
}
}
}
}
}
}
在Express应用中引入Swagger中间件,将文档绑定到指定路径(如/api-docs):
const express = require('express');
const swaggerUi = require('swagger-ui-express');
const YAML = require('yamljs'); // 若使用YAML格式,需安装:npm install yamljs
const app = express();
// 加载Swagger文档(支持JSON/YAML)
const swaggerDocument = YAML.load('./swagger.yaml'); // 或 require('./swagger.json')
// 集成Swagger UI到/api-docs路径
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
// 示例路由
app.get('/api/users', (req, res) => {
res.json([{ id: 1, name: 'John Doe' }]);
});
// 启动服务器
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
console.log(`Swagger UI available at http://localhost:${PORT}/api-docs`);
});
启动Express应用:
node app.js
打开浏览器访问http://<your-server-ip>:3000/api-docs,即可看到Swagger UI界面,展示定义的API文档。
更新系统包列表并安装Java(OpenJDK 11+)、Maven(Java构建工具):
sudo apt update && sudo apt upgrade -y
sudo apt install openjdk-11-jdk maven -y
# 验证安装
java -version # 应输出Java版本(如11.x.x)
mvn -version # 应输出Maven版本(如3.x.x)
使用Spring Initializr创建项目,选择以下依赖:
Spring Boot 3.x + Springdoc OpenAPI,更适配新版Spring Boot)若使用Maven,在pom.xml中添加以下依赖(以springdoc-openapi为例,适配Spring Boot 3.x):
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.5.0</version> <!-- 使用最新稳定版本 -->
</dependency>
若使用Gradle,在build.gradle中添加:
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.5.0'
若使用传统springfox-swagger(Spring Boot 2.x),需创建配置类启用Swagger:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.demo")) // 替换为你的控制器包路径
.paths(PathSelectors.any())
.build();
}
}
若使用springdoc-openapi(推荐,无需额外配置类),默认会自动扫描控制器并生成文档。
在控制器类中使用Swagger注解描述API(以springdoc-openapi为例):
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/api")
public class UserController {
@Operation(summary = "Get all users", description = "Returns a list of all users")
@ApiResponse(responseCode = "200", description = "Successful operation")
@GetMapping("/users")
public List<User> getUsers() {
return List.of(new User(1, "John Doe"), new User(2, "Jane Smith"));
}
}
class User {
private int id;
private String name;
// 构造函数、Getter/Setter省略
}
使用Maven启动应用:
mvn spring-boot:run
启动后,访问http://<your-server-ip>:8080/swagger-ui.html(springfox)或http://<your-server-ip>:8080/swagger-ui/index.html(springdoc),即可查看交互式API文档。
springdoc-openapi适配Spring Boot 3.x,springfox适配Spring Boot 2.x)。securitySchemes配置API认证(如Bearer Token),限制文档访问权限。以上步骤覆盖了Debian系统上Swagger的主要集成场景,可根据实际技术栈选择对应方案。