ubuntu

Ubuntu下Swagger API文档怎么生成

小樊
50
2025-06-19 01:56:48
栏目: 智能运维

在Ubuntu下生成Swagger API文档,通常涉及以下几个步骤:

使用Spring Boot和Springfox

  1. 添加依赖
<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger2</artifactId>
  <version>2.9.2</version>
</dependency>
<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger-ui</artifactId>
  <version>2.9.2</version>
</dependency>
  1. 配置Swagger
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.yourproject"))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Your API Title")
                .description("Your API Description")
                .contact(new Contact("Your Name", "https://www.example.com", "your.email@example.com"))
                .version("1.0")
                .build();
    }
}
  1. 编写API注解
@Api(tags = ["Your API Tag"])
@RestController
@RequestMapping("/your-api")
public class YourController {
    @ApiOperation(value = "Your API Operation", notes = "Your API Notes")
    @GetMapping("/your-endpoint")
    public ResponseEntity<String> yourMethod(@ApiParam(value = "Your Param", required = false) @RequestParam(value = "param", required = false) String param) {
        // Your method implementation
    }
}
  1. 访问Swagger文档

使用Swagger Editor

  1. 安装Node.js和npm(如果尚未安装):
sudo apt update
sudo apt install -y nodejs npm
  1. 下载并安装Swagger Editor
  1. 访问Swagger Editor
  1. 导入或创建Swagger文档
  1. 编辑和查看文档

以上步骤应该可以帮助你在Ubuntu系统下成功生成Swagger API文档。如果你使用的是其他框架或工具,步骤可能会有所不同。

0
看了该问题的人还看了