自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

比試一下:Swagger3就是比2簡單粗暴

開發(fā) 前端
接口文檔總是很煩人,我曾經(jīng)嘗試過用Postman來編寫和分享項(xiàng)目文檔,感覺還不錯(cuò)。但是最近項(xiàng)目緊,我沒有額外的時(shí)間可以花在它上面,這也導(dǎo)致我嘗試YApi(另外一種文檔)的計(jì)劃泡湯了。

[[392715]]

接口文檔總是很煩人,我曾經(jīng)嘗試過用Postman來編寫和分享項(xiàng)目文檔,感覺還不錯(cuò)。但是最近項(xiàng)目緊,我沒有額外的時(shí)間可以花在它上面,這也導(dǎo)致我嘗試YApi(另外一種文檔)的計(jì)劃泡湯了。嗯,目前沒有比Swagger更快、更傻瓜的工具,雖然它有嚴(yán)重的代碼污染。先拿這個(gè)對(duì)付一陣時(shí)間,等閑暇時(shí)間再玩YApi。

Swagger3集成

Swagger目前最新版本是3.0.0,在Spring Boot應(yīng)用中集成Swagger3比老的Swagger2簡單多了,它提供了一個(gè)Starter組件。

  1. <dependency> 
  2.     <groupId>io.springfox</groupId> 
  3.     <artifactId>springfox-boot-starter</artifactId> 
  4.     <version>3.0.0</version> 
  5. </dependency> 

 

就這就可以了,簡單不?

至于有的教程說還要開啟注解@EnableOpenApi,完全不需要。因?yàn)樵趕pringfox-boot-starter-3.0.0.jar下你可以找到一個(gè)spring.factories,熟悉Spring Boot的同學(xué)都知道這個(gè)是一個(gè)Spring Boot 特有的SPI文件,能夠自動(dòng)的發(fā)現(xiàn)并注冊(cè)Starter組件的配置。里面有這樣的配置:

  1. # Auto Configure 
  2. org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ 
  3. springfox.boot.starter.autoconfigure.OpenApiAutoConfiguration 

順藤摸瓜,找到總的配置類OpenApiAutoConfiguration:

  1. @Configuration 
  2. @EnableConfigurationProperties(SpringfoxConfigurationProperties.class) 
  3. @ConditionalOnProperty(value = "springfox.documentation.enabled", havingValue = "true", matchIfMissing = true
  4. @Import({ 
  5.     OpenApiDocumentationConfiguration.class, 
  6.     SpringDataRestConfiguration.class, 
  7.     BeanValidatorPluginsConfiguration.class, 
  8.     Swagger2DocumentationConfiguration.class, 
  9.     SwaggerUiWebFluxConfiguration.class, 
  10.     SwaggerUiWebMvcConfiguration.class 
  11. }) 
  12. @AutoConfigureAfter({ WebMvcAutoConfiguration.class, JacksonAutoConfiguration.class, 
  13.     HttpMessageConvertersAutoConfiguration.class, RepositoryRestMvcAutoConfiguration.class }) 
  14. public class OpenApiAutoConfiguration { 
  15.  

一些發(fā)現(xiàn)

我們找到了關(guān)鍵的一個(gè)地方@ConditionalOnProperty注解聲明了當(dāng)springfox.documentation.enabled為true時(shí)啟用配置,而且默認(rèn)值就是true。這非常有用,Swagger僅僅建議在開發(fā)階段使用,這個(gè)正好是個(gè)開關(guān)。另外有時(shí)候我們自定義配置的時(shí)候最好把這個(gè)開關(guān)也加上:

  1. // 自定義swagger3文檔信息 
  2. @Configuration 
  3. @ConditionalOnProperty(value = "springfox.documentation.enabled", havingValue = "true", matchIfMissing = true
  4. public class Swagger3Config { 
  5.     @Bean 
  6.     public Docket createRestApi() { 
  7.         return new Docket(DocumentationType.OAS_30) 
  8.                 .apiInfo(apiInfo()) 
  9.                 .select() 
  10.                 .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) 
  11.                 .paths(PathSelectors.any()) 
  12.                 .build(); 
  13.     } 
  14.  
  15.     private ApiInfo apiInfo() { 
  16.         return new ApiInfoBuilder() 
  17.                 .title("Swagger3接口文檔"
  18.                 .description("更多請(qǐng)咨詢felord.cn"
  19.                 .contact(new Contact("碼農(nóng)小胖哥""https://felord.cn""dax@felord.cn")) 
  20.                 .version("1.0.0"
  21.                 .build(); 
  22.     } 

如果你想在Swagger3中加入Json Web Token,可以參考這篇文章。

最開始我們提到Swagger3不需要使用@EnableOpenApi或者@EnableSwagger2開啟,這里也能找到答案。

  1. @Import(OpenApiDocumentationConfiguration.class) 
  2. public @interface EnableOpenApi { 
  3. @Import(Swagger2DocumentationConfiguration.class) 
  4. public @interface EnableSwagger2 { 

上面的兩個(gè)導(dǎo)入類都可以在OpenApiAutoConfiguration找到,所以Swagger3提供的是全自動(dòng)的集成。

和全局統(tǒng)一參數(shù)不兼容

如果你使用了統(tǒng)一返回體封裝器來標(biāo)準(zhǔn)化Spring MVC接口的統(tǒng)一返回

  1. /** 
  2.  * 返回體統(tǒng)一封裝器 
  3.  * 
  4.  * @author n1 
  5.  */ 
  6. @RestControllerAdvice  
  7. public class RestBodyAdvice implements ResponseBodyAdvice<Object> { 
  8.     @Override 
  9.     public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) { 
  10.         return !returnType.hasMethodAnnotation(IgnoreRestBody.class); 
  11.     } 
  12.  
  13.     @Override 
  14.     public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) { 
  15.  
  16.         if (body == null) { 
  17.             return RestBody.ok(); 
  18.         } 
  19.         if (Rest.class.isAssignableFrom(body.getClass())) { 
  20.             return body; 
  21.         } 
  22.         return RestBody.okData(body); 
  23.     } 

你會(huì)發(fā)現(xiàn)Swagger3會(huì)報(bào)Unable to infer base url……的錯(cuò)誤,這是因?yàn)榻y(tǒng)一返回體影響到了Swagger3的一些內(nèi)置接口。解決方法是@RestControllerAdvice控制好生效的包范圍,也就是配置其basePackages參數(shù)就行了,這個(gè)潛在的沖突浪費(fèi)我了一個(gè)多小時(shí)。

安全框架放行

如果你使用安全框架,Swagger3的內(nèi)置接口就會(huì)訪問受限,我們需要排除掉。Spring Security是這么配置的:

  1. @Override 
  2. public void configure(WebSecurity web) throws Exception { 
  3.     //忽略swagger3所需要用到的靜態(tài)資源,允許訪問 
  4.     web.ignoring().antMatchers( "/swagger-ui.html"
  5.             "/swagger-ui/**"
  6.             "/swagger-resources/**"
  7.             "/v2/api-docs"
  8.             "/v3/api-docs"
  9.             "/webjars/**"); 

如果你使用的版本是Spring Security 5.4,你可以這么定制WebSecurity:

  1. @Bean 
  2. WebSecurityCustomizer swaggerWebSecurityCustomizer() { 
  3.     return (web) -> { 
  4.         web.ignoring().antMatchers(new String[]{"/swagger-ui.html""/swagger-ui/**""/swagger-resources/**""/v2/api-docs""/v3/api-docs""/webjars/**"}); 
  5.     }; 

更加方便簡單圖片,這樣Swagger就能正常的渲染和訪問了。

總結(jié)

 

 

 

今天分享了一些swagger3的配置心得,希望能夠幫助你上手最新的swagger3文檔工具。

本文轉(zhuǎn)載自微信公眾號(hào)「碼農(nóng)小胖哥」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請(qǐng)聯(lián)系碼農(nóng)小胖哥公眾號(hào)。

 

責(zé)任編輯:武曉燕 來源: 碼農(nóng)小胖哥
相關(guān)推薦

2022-07-21 11:04:53

Swagger3Spring

2024-11-05 09:25:45

2010-12-06 09:10:02

LightSwitch

2023-02-08 09:02:05

VS Code摸魚神器

2022-06-29 10:04:01

PiniaVuex

2020-10-15 11:18:13

Linux內(nèi)核虛擬機(jī)

2022-03-02 10:53:22

Postman工具開發(fā)

2022-12-03 18:24:13

數(shù)據(jù)能力場景

2022-08-08 10:09:08

Vitest單元測試

2009-11-17 11:14:25

Oracle擴(kuò)展

2021-05-07 20:27:14

SpringBootSwagger3文檔

2013-11-20 13:41:32

IE微軟解決方法

2021-01-21 07:31:11

Filter框架權(quán)限

2018-02-08 10:52:13

Kotlin語言代碼

2010-06-13 17:57:23

局域網(wǎng)協(xié)議

2020-07-02 09:46:05

AI

2011-07-20 16:13:03

SQL Profile數(shù)據(jù)庫

2015-11-12 10:32:27

前端后端分離

2009-06-15 11:22:06

2024-01-31 08:23:54

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)