swagger接口文档生成(基础)
引入依赖
<!--swagger依赖-->
<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>
修改配置文件
Spring Boot 2.6.X后与Swagger有版本冲突问题,需要application.properties中加入以下配置
spring:
mvc:
pathmatch:
matching-strategy: ant_path_matcher
编写Swagger配置类
//标明是配置类
@Configuration
//开启Swagger使用(项目注释文档)
@EnableSwagger2
public class Swagger2Config {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
//用于生成API信息
.apiInfo(apiInfo())
//select()函数返回一个ApiSelectorBuilder实例,用来控制接口被swagger做成文档
.select()
//用于指定扫描哪个包下的接口
.apis(RequestHandlerSelectors.basePackage("com"))
//选择所有的API,如果你想只为部分API生成文档,可以配置这里
.paths(PathSelectors.any())
.build();
}
/*
*用于定义API主界面的信息,比如可以声明所有的API的总标题、描述、版本
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
//用来自定义API的标题
.title("SpringBoot项目SwaggerAPI标题测试")
//用来描述整体的API
.description("SpringBoot项目SwaggerAPI描述测试")
//创建人信息
.contact(new Contact("测试员xx","http://xxx","xxx@163.com"))
//用于定义服务的域名
//.termsOfServiceUrl("")
.version("1.0") //可以用来定义版本
.build();
}
}
简单的描述接口
@Api(tags = "文件模块")
@RestController
@RequestMapping("/files")
public class FileUpController {
@ApiOperation(value = "文件上传接口", notes = "用于上传文件")
@PostMapping("/file")
public String up(String nickName, MultipartFile file, HttpServletRequest request){
}