Swagger文档可以在Ubuntu系统上生成,以下是几种常见方法及操作步骤:
Swagger Editor是Swagger官方提供的可视化工具,支持编写、预览和测试OpenAPI规范(YAML/JSON格式)。
sudo npm install -g swagger-ui;swagger.yaml或swagger.json文件;Swagger Codegen可根据OpenAPI规范自动生成多种语言的客户端代码、服务器存根或静态文档。
sudo npm install -g swagger-codegen。swagger-codegen generate -i path/to/swagger.yaml -l html2 -o path/to/output(-i指定输入文件路径,-l指定输出格式如html2、markdown,-o指定输出目录);若项目使用特定框架(如Spring Boot、Nest.js),可通过框架集成的Swagger工具自动生成文档。
pom.xml中加入springfox-swagger2和springfox-swagger-ui;@Configuration类,使用@EnableSwagger2启用Swagger,并设置扫描包路径;@ApiOperation、@ApiParam等注解描述API;http://localhost:8080/swagger-ui.html查看。npm install --save @nestjs/swagger swagger-ui-express;DocumentBuilder创建文档配置,通过SwaggerModule.setup挂载到路由;@ApiTags、@ApiOperation等注解;http://localhost:3000/docs查看。若项目使用Go语言,可通过swag工具根据代码注释自动生成Swagger文档。
go install github.com/swaggo/swag/cmd/swag@latest安装swag工具。swag init,生成docs文件夹(包含docs.go、swagger.json、swagger.yaml);// @Summary、// @Param、// @Success等注释描述API;swag init,更新文档。以上方法均适用于Ubuntu系统,可根据项目需求(如语言、框架、自动化需求)选择合适的方式生成Swagger文档。