Swagger与Debian集成开发主要有以下两种常见场景,具体步骤如下:
安装基础环境
更新系统并安装Java、Maven、Nginx等依赖:
sudo apt update && sudo apt install openjdk-11-jdk maven nginx
创建Spring Boot项目
使用Spring Initializr生成项目,添加Spring Web
和Swagger
依赖(springfox-boot-starter
)。
配置Swagger
application.yml
中启用Swagger:springfox: documentation: swagger-ui: enabled: true
@Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("com.example.controller")) .paths(PathSelectors.any()) .build(); } }
生成并访问文档
启动Spring Boot应用,访问http://localhost:8080/swagger-ui.html
查看API文档。
安装Node.js和npm
sudo apt update && sudo apt install nodejs npm
安装Swagger工具
npm install -g swagger-jsdoc swagger-ui-express
配置Swagger
swagger.json
定义API规范,例如:{ "openapi": "3.0.0", "info": {"title": "API文档", "version": "1.0.0"}, "paths": { "/api/test": { "get": { "summary": "测试接口", "responses": {"200": {"description": "成功响应"}} } } } }
const express = require('express'); const swaggerUi = require('swagger-ui-express'); const swaggerDocument = require('./swagger.json'); const app = express(); app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument)); app.listen(3000, () => console.log('Server running on port 3000'));
运行应用
启动Node.js服务,访问http://localhost:3000/api-docs
查看文档。
springfox
库生成文档,支持注解式配置,适合Java后端。swagger-jsdoc
解析注释生成文档,轻量级且灵活,适合前后端分离架构。