Swagger 提供了一個(gè)全新的維護(hù) API 文檔的方式,有4大優(yōu)點(diǎn):
- 自動(dòng)生成文檔:只需要少量的注解,Swagger 就可以根據(jù)代碼自動(dòng)生成 API 文檔,很好的保證了文檔的時(shí)效性。
- 跨語言性,支持 40 多種語言。
- Swagger UI 呈現(xiàn)出來的是一份可交互式的 API 文檔,我們可以直接在文檔頁面嘗試 API 的調(diào)用,省去了準(zhǔn)備復(fù)雜的調(diào)用參數(shù)的過程。
- 還可以將文檔規(guī)范導(dǎo)入相關(guān)的工具(例如 SoapUI), 這些工具將會(huì)為我們自動(dòng)地創(chuàng)建自動(dòng)化測試。
如何實(shí)現(xiàn)swagger
一: pom文件加入依賴包
1
2
3
4
5
6
7
8
9
10
11
12
|
<!--swagger--> < dependency > < groupId >io.springfox</ groupId > < artifactId >springfox-swagger2</ artifactId > < version >2.9.2</ version > </ dependency > <!--swagger-ui--> < dependency > < groupId >io.springfox</ groupId > < artifactId >springfox-swagger-ui</ artifactId > < version >2.9.2</ version > </ dependency > |
二:修改配置文件
1.application.properties 加入配置
1
2
|
#表示是否開啟 Swagger,一般線上環(huán)境是關(guān)閉的 spring.swagger2.enabled= true |
2.增加一個(gè)swagger配置類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
@Configuration @EnableSwagger2 public class SwaggerConfig { @Value (value = "${spring.swagger2.enabled}" ) private Boolean swaggerEnabled; @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .enable(swaggerEnabled) .select() .apis(RequestHandlerSelectors.basePackage( "com.swagger.boot" )) //包名代表需要生成接口文檔的目錄包。 .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title( "接口文檔" ) .description( " Spring Boot" ) .version( "1.0" ) .build(); } } |
以上就是Springboot集成swagger實(shí)現(xiàn)方式的詳細(xì)內(nèi)容,更多關(guān)于Springboot集成swagger的資料請關(guān)注服務(wù)器之家其它相關(guān)文章!
原文鏈接:https://blog.csdn.net/qq_44807716/article/details/120005709