Spring Boot中使用Swagger生成接口说明书
1. 添加Swagger依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.5.0</version>
</dependency>
2. 添加配置类
@Configuration
@EnableSwagger2
public class SwaggerConf {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//basePackage路径为需要扫描的包,就是配置过注解,需要生成文档的包。一般是controller的包名
.apis(RequestHandlerSelectors.basePackage("com.huo.mybatisplus.controller"))
.paths(PathSelectors.any())
.build();
}
//构建 api文档的详细信息函数,注意这里的注解引用的是哪个
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
//页面标题
.title("RESTful API")
//创建人
.contact(new Contact("Jeff", "https://test.github.io", "1234@qq.com"))
//版本号
.version("1.0")
//描述
.description("各接口说明书")
.build();
}
}
3. 添加注解及说明
3.1 @Api:用在请求的类上,说明该类的作用
value: 没有什么用,可以不用配置
tags: 这个类的说明
@Api(value = "userController",tags = {"用户信息获取"})
@Controller
public class UserController {
}
3.2 @ApiOperation:用在请求的方法上
value: 方法的概略说明
notes: 方法的详细说明
@ApiOperation(value = "获取所有的用户",notes = "传递id参数,获取用户信息")
3.3 @ApiImplicitParams:用在请求的方法上,请求参数说明
@ApiImplicitParams({
@ApiImplicitParam(name = "telephone", value = "电话号码", paramType = "query", required = true, dataType = "Integer"),
@ApiImplicitParam(name = "name", value = "姓名", paramType = "query", required = true, dataType = "Integer")
})