首頁/ 汽車/ 正文

Spring Boot 進階-詳解Spring Boot中使用Swagger3.0

Spring Boot 進階-詳解Spring Boot中使用Swagger3.0

在上篇文章中我們介紹了Spring Boot 整合Swagger3。0的一些基礎用法,這篇文章中我們來深入學習一下Swagger3。0 還有其他高階用法。

在日常的開發中,為了減少工作量,我們會遇到一種情況,就是將前端的介面與後端的介面編寫到同一個程式碼中,這樣也提高了程式碼的複用率,減少了重複開發程式碼的工作量。這個時候我們就需要將介面文件也進行拆分。分為前端介面和後端介面兩個部分。

在Swagger3。0中如何進行介面分組呢?

Swagger3。0 介面分組操作

透過之前的例子我們知道,我們往容器中注入一個Docket就是注入了一個文件,其中我們設定了groupName的分組名稱。要想注入多個文件,意思就是需要往容器中注入多個Docket。既然是這樣,我們就需要修改之前的Swagger的配置檔案,將其改為如下的程式碼。

@EnableOpenApi@Configuration@EnableConfigurationProperties(value = {SwaggerProperties。class})public class Swagger3Config { @Autowired private SwaggerProperties swaggerProperties; @Bean public Docket frontApi(){ return new Docket(DocumentationType。OAS_30) // 是否開啟,根據環境配置 。enable(swaggerProperties。getFront()。getEnable()) 。groupName(swaggerProperties。getFront()。getGroupName()) 。apiInfo(frontApiInfo()) 。select() 。apis(RequestHandlerSelectors。basePackage(swaggerProperties。getFront()。getBasePackege())) 。paths(PathSelectors。any()) 。build(); } // 前臺API資訊 private ApiInfo frontApiInfo(){ return new ApiInfoBuilder() 。title(swaggerProperties。getFront()。getTitle()) 。description(swaggerProperties。getFront()。getDescription()) 。version(swaggerProperties。getFront()。getVersion()) 。contact( // 新增開發者資訊 new Contact( swaggerProperties。getFront()。getContactName(), swaggerProperties。getFront()。getContactUrl(), swaggerProperties。getFront()。getContactEmail()) )。build(); } @Bean public Docket backApi(){ return new Docket(DocumentationType。OAS_30) // 是否開啟,根據環境配置 。enable(swaggerProperties。getBack()。getEnable()) 。groupName(swaggerProperties。getBack()。getGroupName()) 。apiInfo(backApiInfo()) 。select() 。apis(RequestHandlerSelectors。basePackage(swaggerProperties。getBack()。getBasePackege())) 。paths(PathSelectors。any()) 。build(); } // 前臺API資訊 private ApiInfo backApiInfo(){ return new ApiInfoBuilder() 。title(swaggerProperties。getBack()。getTitle()) 。description(swaggerProperties。getBack()。getDescription()) 。version(swaggerProperties。getBack()。getVersion()) 。contact( // 新增開發者資訊 new Contact( swaggerProperties。getBack()。getContactName(), swaggerProperties。getBack()。getContactUrl(), swaggerProperties。getBack()。getContactEmail()) )。build(); }}

我們會看到在這個配置類中注入了兩個Docket,分別代表前端的介面和後端的介面,那麼在配置檔案中如何進行配置呢?

首先我們先來看一下其配置類,程式碼如下

@Data@ConfigurationProperties(prefix = “spring。swagger3”)public class SwaggerProperties { private SwaggerEntity front; private SwaggerEntity back; @Data public static class SwaggerEntity{ private String groupName; private String basePackege; private String title; private String description; private String contactName; private String contactEmail; private String contactUrl; private String version; private Boolean enable; }}

全域性配置類

spring: swagger3: front: enable: true groupName: 前端介面 basePackege: com。example。demo。controller。api title: 前端測試介面 description: 前端測試Spring Boot整合Swagger3。0 contactName: nihui contactEmail: 18202504057@163。com contactUrl: https://blog。csdn。net/nihui123 version: 0。0。1 back: enable: true groupName: 後端介面 basePackege: com。example。demo。controller。web title: 後端測試介面 description: 後端測試Spring Boot整合Swagger3。0 contactName: nihui contactEmail: 18202504057@163。com contactUrl: https://blog。csdn。net/nihui123 version: 0。0。1 mvc: pathmatch: matching-strategy: ant_path_matcher

透過上面的配置完成配置之後,就會看到在介面頁面會有兩個分組,一個是前端介面、一個是後端介面分組。

Spring Boot 進階-詳解Spring Boot中使用Swagger3.0

新增授權認證資訊

在很多對外提供服務的API介面中,都會有一個授權認證的操作,否則這些介面是無法進行呼叫的。例如在一般的情況下,會在介面中包含一個TOKEN引數。

那麼在Swagger3。0中如何對這個認證資訊進行配置,這樣在每次請求的時候都會帶著這個引數進行請求,這樣就避免了我們在測試過程中,每次都需要新增這個資訊的麻煩。

要完成授權資訊的新增,我們需要在Docket方法中新增如下的內容

@Configurationpublic class BaseSwagger3Config { @Autowired private BaseSwagger3Properties properties; @Bean public Docket baseApi(){ return new Docket(DocumentationType。OAS_30) // 是否開啟,根據環境配置 。enable(properties。getEnable()) 。groupName(properties。getGroupName()) 。apiInfo(frontApiInfo()) 。select() 。apis(RequestHandlerSelectors。basePackage(properties。getBasePackege())) 。paths(PathSelectors。any()) 。build() 。securitySchemes(securitySchemes()) 。securityContexts(securityContexts()); } private List securitySchemes(){ ApiKey apiKey = new ApiKey(“TOKEN”,“token”, In。HEADER。toValue()); return Collections。singletonList(apiKey); } private List securityContexts(){ return Collections。singletonList(SecurityContext。builder()。 securityReferences(Collections。singletonList( new SecurityReference(“TOKEN”,new AuthorizationScope[] {new AuthorizationScope(“global”,“”)})))。build()); } // 前臺API資訊 private ApiInfo frontApiInfo(){ return new ApiInfoBuilder() 。title(properties。getTitle()) 。description(properties。getDescription()) 。version(properties。getVersion()) 。contact( // 新增開發者資訊 new Contact( properties。getContactName(), properties。getContactUrl(), properties。getContactEmail()) )。build(); }}

完成上述配置之後,會看到在介面頁面上會多出一個認證的按鈕。透過這個認證的按鈕我們可以設定對應的引數值

Spring Boot 進階-詳解Spring Boot中使用Swagger3.0

新增公共引數

在不同的平臺使用的時候,除了攜帶請求Token之外還會有一些使用者進行使用者分析,大資料分析的引數被傳到請求中。那麼如何在Swagger3。0中去定義這些公共引數呢?這個時候我們需要在配置程式碼中新增如下的內容。

@Configurationpublic class BaseSwagger3Config { @Autowired private BaseSwagger3Properties properties; @Bean public Docket baseApi(){ RequestParameter requestParameter = new RequestParameterBuilder() 。name(“clientType”) 。description(“客戶端型別”) 。in(ParameterType。HEADER) 。required(true) 。build(); //建立一個請求引數集合 List requestParameterList = Collections。singletonList(requestParameter); return new Docket(DocumentationType。OAS_30) // 是否開啟,根據環境配置 。enable(properties。getEnable()) 。groupName(properties。getGroupName()) 。apiInfo(frontApiInfo()) 。select() 。apis(RequestHandlerSelectors。basePackage(properties。getBasePackege())) 。paths(PathSelectors。any()) 。build() 。globalRequestParameters(requestParameterList) 。securitySchemes(securitySchemes()) 。securityContexts(securityContexts()); } private List securitySchemes(){ ApiKey apiKey = new ApiKey(“TOKEN”,“token”, In。HEADER。toValue()); return Collections。singletonList(apiKey); } private List securityContexts(){ return Collections。singletonList(SecurityContext。builder()。 securityReferences(Collections。singletonList( new SecurityReference(“TOKEN”,new AuthorizationScope[] {new AuthorizationScope(“global”,“”)})))。build()); } // 前臺API資訊 private ApiInfo frontApiInfo(){ return new ApiInfoBuilder() 。title(properties。getTitle()) 。description(properties。getDescription()) 。version(properties。getVersion()) 。contact( // 新增開發者資訊 new Contact( properties。getContactName(), properties。getContactUrl(), properties。getContactEmail()) )。build(); }}

會看到上述程式碼中透過,RequestParameter定義了一個公共的引數,並且將其放入到了一個引數集合中,透過Docket的。globalRequestParameters(requestParameterList)方法將其新增到Swagger3。0文件中。然後執行專案會看到在介面中就會攜帶這對應的引數出現,並且這個引數是全域性的,也就是說每個介面中都需要有這一個引數。

Spring Boot 進階-詳解Spring Boot中使用Swagger3.0

到這裡,Swagger的相關內容就告一段落了。細心的讀者會發現,在前面我們新增分組配置的時候在配置類上添加了好多的註解,但是在我們演示認證資訊與全域性引數的時候,配置類中只是簡單的添加了一個@Configuration 註解。這是為什麼呢?下面我們就來探索一下原始碼。

OpenApiAutoConfiguration 原始碼

會看到在這段原始碼中,好像什麼功能都沒實現,但是它確完成了Swagger3。0大多數的配置,而對於@EnableOpenApi 註解來講。

@Configuration@EnableConfigurationProperties(SpringfoxConfigurationProperties。class)@ConditionalOnProperty(value = “springfox。documentation。enabled”, havingValue = “true”, matchIfMissing = true)@Import({ OpenApiDocumentationConfiguration。class, SpringDataRestConfiguration。class, BeanValidatorPluginsConfiguration。class, Swagger2DocumentationConfiguration。class, SwaggerUiWebFluxConfiguration。class, SwaggerUiWebMvcConfiguration。class})@AutoConfigureAfter({ WebMvcAutoConfiguration。class, JacksonAutoConfiguration。class, HttpMessageConvertersAutoConfiguration。class, RepositoryRestMvcAutoConfiguration。class })public class OpenApiAutoConfiguration {}

@EnableOpenApi 註解

對於@EnableOpenApi註解來講其關鍵的程式碼就是@Import(OpenApiDocumentationConfiguration。class) 引入的還是這個類。

@Retention(value = java。lang。annotation。RetentionPolicy。RUNTIME)@Target(value = {java。lang。annotation。ElementType。TYPE})@Documented@Import(OpenApiDocumentationConfiguration。class)public @interface EnableOpenApi {}

這裡我們會發現為什麼在沒有@EnableOpenApi註解的時候也會生效呢?那就是用到了我們Spring Boot自動配置的原理了。關於自動配置的原理在後續的分析中我們來進行講解,這裡就先不做過多說明。提示大家的就是找到如下圖中的程式碼就可以理解自動配置的原理

Spring Boot 進階-詳解Spring Boot中使用Swagger3.0

總結

透過上面的內容我們瞭解了Swagger3。0的一些擴充套件性的用法,當然Swagger的用法遠遠不止這些,但是這些內容已經可以支援在我們日常開發中的一些工作。希望大家多多關注,筆者會為大家帶來更多精彩幹活

相關文章

頂部