av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

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

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

創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供石家莊網(wǎng)站建設、石家莊做網(wǎng)站、石家莊網(wǎng)站設計、石家莊網(wǎng)站制作等企業(yè)網(wǎng)站建設、網(wǎng)頁設計與制作、石家莊企業(yè)網(wǎng)站模板建站服務,十載石家莊做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡服務。

Swagger3集成

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

 
 
 
 
  1.     io.springfox
  2.     springfox-boot-starter
  3.     3.0.0

就這就可以了,簡單不?

至于有的教程說還要開啟注解@EnableOpenApi,完全不需要。因為在springfox-boot-starter-3.0.0.jar下你可以找到一個spring.factories,熟悉Spring Boot的同學都知道這個是一個Spring Boot 特有的SPI文件,能夠自動的發(fā)現(xiàn)并注冊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)鍵的一個地方@ConditionalOnProperty注解聲明了當springfox.documentation.enabled為true時啟用配置,而且默認值就是true。這非常有用,Swagger僅僅建議在開發(fā)階段使用,這個正好是個開關(guān)。另外有時候我們自定義配置的時候最好把這個開關(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.     private ApiInfo apiInfo() {
  15.         return new ApiInfoBuilder()
  16.                 .title("Swagger3接口文檔")
  17.                 .description("更多請咨詢felord.cn")
  18.                 .contact(new Contact("碼農(nóng)小胖哥", "https://felord.cn", "[email protected]"))
  19.                 .version("1.0.0")
  20.                 .build();
  21.     }
  22. }

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

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

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

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

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

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

 
 
 
 
  1. /**
  2.  * 返回體統(tǒng)一封裝器
  3.  *
  4.  * @author n1
  5.  */
  6. @RestControllerAdvice 
  7. public class RestBodyAdvice implements ResponseBodyAdvice {
  8.     @Override
  9.     public boolean supports(MethodParameter returnType, Class> converterType) {
  10.         return !returnType.hasMethodAnnotation(IgnoreRestBody.class);
  11.     }
  12.     @Override
  13.     public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
  14.         if (body == null) {
  15.             return RestBody.ok();
  16.         }
  17.         if (Rest.class.isAssignableFrom(body.getClass())) {
  18.             return body;
  19.         }
  20.         return RestBody.okData(body);
  21.     }
  22. }
  23. 你會發(fā)現(xiàn)Swagger3會報Unable to infer base url……的錯誤,這是因為統(tǒng)一返回體影響到了Swagger3的一些內(nèi)置接口。解決方法是@RestControllerAdvice控制好生效的包范圍,也就是配置其basePackages參數(shù)就行了,這個潛在的沖突浪費我了一個多小時。

    安全框架放行

    如果你使用安全框架,Swagger3的內(nèi)置接口就會訪問受限,我們需要排除掉。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/**");
    10. }

    如果你使用的版本是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.     };
    6. }

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

    總結(jié)

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

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


    網(wǎng)站標題:比試一下:Swagger3就是比2簡單粗暴
    URL標題:http://uogjgqi.cn/article/dphpopg.html
    掃二維碼與項目經(jīng)理溝通

    我們在微信上24小時期待你的聲音

    解答本文疑問/技術(shù)咨詢/運營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流