Spring Boot整合Swagger2
swagger2 是一個(gè)規(guī)范和完整的框架,用于生成、描述、調(diào)用和可視化Restful風(fēng)格的web服務(wù)。
作用:
1、接口的文檔在線自動(dòng)生成
2、功能測(cè)試
Spring Boot整合Swagger2
1.添加Swagger相關(guān)依賴
</dependency>
<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>
2.配置Swagger
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.swagger.controller"))
.paths(PathSelectors.any())
.build().apiInfo(new ApiInfoBuilder()
.description("接口文檔描述信息")
.title("xx項(xiàng)目接口文檔")
.contact(new Contact("zby","http://www.baidu.com","578499116@qq.com"))
.version("v1.0")
.license("Apache2.0")
.build());
}
}
寫好測(cè)試類和測(cè)試接口后啟動(dòng)項(xiàng)目,訪問localhost:8080/swagger-ui.html即可看到接口文檔信息
image
也可以在該頁面進(jìn)行接口測(cè)試。
可以在接口中利用swagger2提供的注解對(duì)接口及接口中的方法進(jìn)行描述
@Api(tags = "xxx"):對(duì)接口功能進(jìn)行描述
@ApiOperation(value = "xxx",notes = "xxx"):對(duì)接口中方法的描述
@ApiImplicitParam(name = "xxx",value = "xxx" ):對(duì)參數(shù)進(jìn)行描述