spring boot集成swagger2
前言
最近公司要搭建一个spring boot的项目,要求继承swagger2。Swagger2是一款RESTFUL接口的文档在线自动生成和功能测试功能软件,它具有在线测试的功能。下面介绍集成swagger的操作:
添加依赖
在pom文件中引入swagger的依赖
<!--swagger依赖-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
swagger的配置类
@Configuration //声明为配置类
@EnableSwagger2 // 声明为swagger
public class SwaggerConfig {
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
// 设置basePackage会将包下的所有被@Api标记类的所有方法作为api
.apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
//只有标记了@ApiOperation的方法才会暴露出给swagger
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build()