1. Swagger簡介
1.1 前后端分離發展歷史
后端時代:
前段只用管靜態頁面;html==>后端,模版引擎JSP=>后端是助理
前后端分離時代:
后端:后端控制層,服務層,資料訪問層次【后端團隊】
前段:前端控制層,視圖層【前段團隊】, 偽造后端資料json已經存在了,不需要后端,前段工程依舊可以跑起來,
那么問題來了?
前后端如何互動?===》API
前后端相對獨立,松耦合
前后端甚至可以部署在不同的服務器上
生產一個問題:
前后端集成聯調,前后端人員無法做到“及時協商,盡早解決”,導致問題集中爆發,
首先指定一個schema,實時更新最新的api,降低集成風險,
早些年,制定word檔案
前后端分離:前端測驗后端介面:post; 后端提供介面,需要實時更新最新的訊息以及改動
1.2 Swagger產生
Swagger:
- 號稱世界上最流行的API框架
- Restful Api檔案在線自動生成工具=>API檔案與API定義同步更新
- 直接運行,在線測驗API介面
- 支持多種語言
- 官網地址:https://swagger.io/
2. SpringBoot集成Swagger
2.1初步集成swagger
-
新建一個Springboot-web專案,撰寫一個hello工程
-
匯入相關依賴
1 <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 --> 2 <dependency> 3 <groupId>io.springfox</groupId> 4 <artifactId>springfox-swagger2</artifactId> 5 <version>2.9.2</version> 6 </dependency> 7 <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui --> 8 <dependency> 9 <groupId>io.springfox</groupId> 10 <artifactId>springfox-swagger-ui</artifactId> 11 <version>2.9.2</version> 12 </dependency>
- 配置Swagger
1 @Configuration //配置類 2 @EnableSwagger2// 開啟Swagger2的自動配置 3 public class SwaggerConfig { 4 }
- 測驗運行 http://localhost:8080/swagger-ui.html ;包含四部分:Swagger資訊 介面資訊 物體資訊 組
- 配置Swagger掃描介面
1 package com.study.cloud.config; 2 3 import org.springframework.context.annotation.Bean; 4 import org.springframework.context.annotation.Configuration; 5 import org.springframework.context.annotation.Profile; 6 import org.springframework.core.env.Environment; 7 import org.springframework.core.env.Profiles; 8 import springfox.documentation.RequestHandler; 9 import springfox.documentation.builders.PathSelectors; 10 import springfox.documentation.builders.RequestHandlerSelectors; 11 import springfox.documentation.service.ApiInfo; 12 import springfox.documentation.service.Contact; 13 import springfox.documentation.spi.DocumentationType; 14 import springfox.documentation.spring.web.plugins.Docket; 15 import springfox.documentation.swagger2.annotations.EnableSwagger2; 16 17 import java.util.ArrayList; 18 19 @Configuration //配置類 20 @EnableSwagger2// 開啟Swagger2的自動配置 21 public class SwaggerConfig { 22 @Bean 23 public Docket docket(Environment environment){ 24 Profiles profiles = Profiles.of("dev","test"); 25 boolean flag = environment.acceptsProfiles(profiles); 26 return new Docket(DocumentationType.SWAGGER_2) 27 .apiInfo(apiInfo()) 28 .groupName("Cloud2022組")//分組名稱不能有空格 29 .enable(flag)//通過enable()方法配置是否啟用swagger,如果是false,swagger將不能在瀏覽器中訪問了 30 .select()// 通過.select()方法,去配置掃描介面,RequestHandlerSelectors配置如何掃描介面 31 /* 32 any() // 掃描所有,專案中的所有介面都會被掃描到 33 none() // 不掃描介面 34 withMethodAnnotation(final Class<? extends Annotation> annotation)// 通過方法上的注解掃描 35 withClassAnnotation(final Class<? extends Annotation> annotation)// 通過類上的注解掃描, 36 basePackage(final String basePackage) // 根據包路徑掃描介面; 37 */ 38 .apis(RequestHandlerSelectors.any()) 39 /* any() // 任何請求都掃描 40 none() // 任何請求都不掃描 41 regex(final String pathRegex) // 通過正則運算式控制 42 ant(final String antPattern) // 通過ant()控制 * */ 43 .paths(PathSelectors.ant("/**"))// // 配置如何通過path過濾,即這里只掃描請求以/kuang開頭的介面 44 .build(); 45 46 } 47 @Bean 48 public Docket docketA( ){ 49 return new Docket(DocumentationType.SWAGGER_2).groupName("A"); 50 } 51 @Bean 52 public Docket docketB( ){ 53 return new Docket(DocumentationType.SWAGGER_2).groupName("B"); 54 } 55 @Bean 56 public Docket docketC( ){ 57 return new Docket(DocumentationType.SWAGGER_2).groupName("C"); 58 } 59 @Bean 60 public Docket docketD( ){ 61 return new Docket(DocumentationType.SWAGGER_2).groupName("D"); 62 } 63 64 private ApiInfo apiInfo(){ 65 Contact contact = new Contact("gzm", "", ""); 66 return new ApiInfo("Cloud 2022 API檔案" 67 , "描述:API檔案描述!" 68 , "1.0" 69 , "urn:tos" 70 , contact 71 , "Apache 2.0" 72 , "http://www.apache.org/licenses/LICENSE-2.0" 73 , new ArrayList()); 74 } 75 76 77 }
-
@Api(tags = "xxx模塊說明")作用在模塊類上
@ApiOperation("xxx介面說明")作用在介面方法上
@ApiModel("xxxPOJO說明")作用在模型類上:如VO、BO
@ApiModelProperty(value = "https://www.cnblogs.com/wonderfulpersuit/archive/2022/09/18/xxx屬性說明",hidden = true)作用在類方法和屬性上,hidden設定為true可以隱藏該屬性@ApiParam("xxx引數說明")作用在引數、方法和欄位上, 類似@ApiModelProperty
相較于傳統的Postman或Curl方式測驗介面,使用swagger簡直就是傻瓜式操作,不需要額外說明檔案(寫得好本身就是檔案)而且更不容易出錯,只需要錄入資料然后點擊Execute,如果再配合自動化框架,可以說基本就不需要人為操作了,
Swagger是個優秀的工具,現在國內已經有很多的中小型互聯網公司都在使用它,相較于傳統的要先出Word介面檔案再測驗的方式,顯然這樣也更符合現在的快速迭代開發行情,當然了,提醒下大家在正式環境要記得關閉Swagger,一來出于安全考慮二來也可以節省運行時記憶體,
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/509056.html
標籤:其他
上一篇:Git 常用命令總結
