成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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

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

[[392715]]

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

Swagger3集成

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

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

 

就這就可以了,簡單不?

至于有的教程說還要開啟注解@EnableOpenApi,完全不需要。因為在springfox-boot-starter-3.0.0.jar下你可以找到一個spring.factories,熟悉Spring Boot的同學都知道這個是一個Spring Boot 特有的SPI文件,能夠自動的發現并注冊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.  

一些發現

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

  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("更多請咨詢felord.cn"
  19.                 .contact(new Contact("碼農小胖哥""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 { 

上面的兩個導入類都可以在OpenApiAutoConfiguration找到,所以Swagger3提供的是全自動的集成。

和全局統一參數不兼容

如果你使用了統一返回體封裝器來標準化Spring MVC接口的統一返回

  1. /** 
  2.  * 返回體統一封裝器 
  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.     } 

你會發現Swagger3會報Unable to infer base url……的錯誤,這是因為統一返回體影響到了Swagger3的一些內置接口。解決方法是@RestControllerAdvice控制好生效的包范圍,也就是配置其basePackages參數就行了,這個潛在的沖突浪費我了一個多小時。

安全框架放行

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

  1. @Override 
  2. public void configure(WebSecurity web) throws Exception { 
  3.     //忽略swagger3所需要用到的靜態資源,允許訪問 
  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就能正常的渲染和訪問了。

總結

 

 

 

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

本文轉載自微信公眾號「碼農小胖哥」,可以通過以下二維碼關注。轉載本文請聯系碼農小胖哥公眾號。

 

責任編輯:武曉燕 來源: 碼農小胖哥
相關推薦

2022-07-21 11:04:53

Swagger3Spring

2024-11-05 09:25:45

2010-12-06 09:10:02

LightSwitch

2022-06-29 10:04:01

PiniaVuex

2023-02-08 09:02:05

VS Code摸魚神器

2022-03-02 10:53:22

Postman工具開發

2020-10-15 11:18:13

Linux內核虛擬機

2022-08-08 10:09:08

Vitest單元測試

2022-12-03 18:24:13

數據能力場景

2009-11-17 11:14:25

Oracle擴展

2021-05-07 20:27:14

SpringBootSwagger3文檔

2021-01-21 07:31:11

Filter框架權限

2013-11-20 13:41:32

IE微軟解決方法

2018-02-08 10:52:13

Kotlin語言代碼

2010-06-13 17:57:23

局域網協議

2020-07-02 09:46:05

AI

2011-07-20 16:13:03

SQL Profile數據庫

2015-11-12 10:32:27

前端后端分離

2009-06-15 11:22:06

2024-01-31 08:23:54

點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 欧美精品在线免费 | 国产精品久久久久久久久久久免费看 | 国产亚洲成av人片在线观看桃 | 麻豆精品国产91久久久久久 | 国产一区二区三区精品久久久 | 日本精品一区二区三区在线观看视频 | 天堂色区 | 久久精品日产第一区二区三区 | 久久一 | 免费在线观看av网站 | 日韩电影一区二区三区 | 国产丝袜av | 99精品久久| 羞羞色影院 | www.日韩系列 | 一区二区三区在线观看视频 | 日韩av资源站 | www.亚洲精品 | 国产区视频在线观看 | 精品免费国产一区二区三区四区 | 精品国产一区二区在线 | 国产精品免费一区二区三区四区 | 亚洲国产精品久久久 | 久久久久久久综合 | 男女爱爱网站 | 男女在线免费观看 | 欧洲视频一区二区 | 欧美1区2区 | 久久久久国产精品 | 九九福利 | 日韩免费中文字幕 | 日日干天天干 | 精品人伦一区二区三区蜜桃网站 | 天天看逼 | 在线区| 国产日韩欧美中文字幕 | 国产成人免费视频网站高清观看视频 | 午夜影院视频在线观看 | 日韩成人免费中文字幕 | 久久久青草婷婷精品综合日韩 | 亚洲精品乱码 |