初学SpringBoot集成Swagger的使用
SpringBoot 集成Swagger
1.新建一个SpringBoot 项目
2.导入依赖
有两个依赖
Swagger2
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
Swagger ui
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
3.配置Swagger ==》config
@Configuration
@EnableSwagger2
public class SwaggerConfig {
}
4.测试运行
项目跑起来后,即可去浏览器键入下面的地址,就可以看到Swagger的接口管理页面
http://localhost:8080/swagger-ui.html
SwaggerConfig配置文件内容
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//RequestHandlerSelectors()选择扫描方式
//加了ApiOperation注解的类,才生成接口文档
.apis(RequestHandlerSelectors.withClassAnnotation(ApiOperation.class))
//扫描指定包下的类,才生成接口文档
.apis(RequestHandlerSelectors.basePackage("cn.cnkimall.modules.sys.controller"))
.paths(PathSelectors.any())
.build()
.securitySchemes(security());
}
private ApiInfo apiInfo(String title, String version, String description) {
return new ApiInfoBuilder()
.title(title) //标题
.description(description) //说明
.version(version) //版本号
.build();
}