在Ubuntu系统下生成Swagger API文档,通常需要根据你所使用的编程语言和框架来选择相应的工具和方法。以下是几种常见的方法:
sudo apt update
sudo apt install nodejs npm
下载并安装Swagger Editor:
http-server
。访问Swagger Editor:
打开浏览器,访问 http://localhost:8080
(具体端口可能根据你的设置有所不同)。
导入或创建Swagger文档: 你可以导入现有的Swagger JSON或YAML文件,或者创建一个新的文档。
编辑和查看文档: 在Swagger Editor中直接编辑你的API文档,然后保存并查看。
go install github.com/swaggo/swag/cmd/swag@latest
docs.go
文件,其中包含了Swagger文档的代码。swag init
// @Summary 获取用户信息
// @Description 获取用户信息
// @Tags Users
// @Accept json
// @Produce json
// @Param id path int true "用户ID"
// @Success 200 {object} User
// @Router /users/{id} [get]
swag init
命令,swag工具会根据你的注释生成相应的Swagger文档。pom.xml
文件中添加Springfox的依赖。<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
@EnableSwagger2
注解启用Swagger,并配置文档的基本信息和扫描包的路径。@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("cn.weijishu.server.api.rest"))
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Weijishu Swagger API Document")
.description("Weijishu Swagger Description...")
.contact(new Contact("Weijishu", "https://www.weijisu.cn/", "mail@weijishu.cn"))
.version("0.1")
.build();
}
}
@Api(tags ["SwaggerDemo"])
@RestController
@RequestMapping("/weijishu")
public class WeijishuSwagger2Controller {
@ApiOperation(value = "方法说明", notes = "通过A")
@PostMapping("/add")
public ResponseEntity<String> add(@RequestBody String requestBody) {
// 你的业务逻辑
return ResponseEntity.ok("Success");
}
}
http://localhost:8080/swagger-ui.html
访问生成的Swagger文档。希望这些信息能帮助你在Ubuntu系统下成功生成Swagger API文档。