日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
比試一下:Swagger3就是比2簡單粗暴

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

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

Swagger3集成

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

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

就這就可以了,簡單不?

至于有的教程說還要開啟注解@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)并注冊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.     private ApiInfo apiInfo() {
  15.         return new ApiInfoBuilder()
  16.                 .title("Swagger3接口文檔")
  17.                 .description("更多請(qǐng)咨詢felord.cn")
  18.                 .contact(new Contact("碼農(nóng)小胖哥", "https://felord.cn", "dax@felord.cn"))
  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. }

上面的兩個(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 {
  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. 你會(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/**");
    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)載自微信公眾號(hào)「碼農(nóng)小胖哥」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請(qǐng)聯(lián)系碼農(nóng)小胖哥公眾號(hào)。


    當(dāng)前文章:比試一下:Swagger3就是比2簡單粗暴
    地址分享:http://www.5511xx.com/article/dphpopg.html