Swagger使用總結


Swagger使用總結https://www.cnblogs.com/h9527/p/5506956.html

1. Swagger是什么?

官方說法:Swagger是一個規范和完整的框架,用於生成、描述、調用和可視化 RESTful 風格的 Web 服務。總體目標是使客戶端和文件系統作為服務器以同樣的速度來更新。文件的方法,參數和模型緊密集成到服務器端的代碼,允許API來始終保持同步。

個人覺得,swagger的一個最大的優點是能實時同步api與文檔。在項目開發過程中,發生過多次:修改代碼但是沒有更新文檔,前端還是按照老舊的文檔進行開發,在聯調過程中才發現問題的情況(當然依據開閉原則,對接口的修改是不允許的,但是在項目不穩定階段,這種情況很難避免)。

2. spring boot 集成 Swagger

目前維護的系統是基於springboot框架開發的,因此本文會詳細描述springboot與swagger集成的過程。

spring-boot系統集成swagger需要進行如下操作:

  1. 添加maven依賴,需要在系統的pom中添加如下依賴:

    gradle:compile('io.springfox:springfox-swagger2:2.2.2')
     <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.2.2</version> </dependency> 
  2. 添加swagger配置文件,配置文件如下:

  3. import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;

    import springfox.documentation.builders.PathSelectors;
    import springfox.documentation.builders.RequestHandlerSelectors;
    import springfox.documentation.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;

    @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() // 選擇那些路徑和api會生成document .apis(RequestHandlerSelectors.any()) // 對所有api進行監控 .paths(PathSelectors.any()) // 對所有路徑進行監控 .build(); } }

    通過注解EnableSwagger2聲明Swagger的可用性,此處會定義一個類型為Docket的bean,
    關於docket類的說明如下:

    A builder which is intended to be the primary interface into the swagger-springmvc framework.Provides sensible defaults and convenience methods for configuration.

    Docket的select()方法會提供給swagger-springmvc framework的一個默認構造器(ApiSelectorBuilder),這個構造器為配置swagger提供了一系列的默認屬性和便利方法。

  4. 測試

    通過訪問:http://localhost:8080/your-app-root/v2/api-docs ,能測試生成的api是否可用。此時返回的是一個json形式的頁面,可讀性不好。可以通過Swagger UI來生成一個可讀性良好的api頁面。具體做法就是,在pom中添加相關依賴。如下:

    <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.2.2</version> </dependency>
  5.   gradle:  compile('io.springfox:springfox-swagger-ui:2.2.2')
  6. 再次訪問:http://localhost:8080/your-app-root/swagger-ui.html 就可以看到可讀性較好的api文檔頁面。

  7. 注意:

    1. http://localhost:8080/your-app-root/v2/api-docs 中your-app-root指的你的wabapp的根路徑,我目前的webapp就默認在根路徑下,所以地址是:http://localhost:8080/v2/api-docs
    2. spring-mvc上引用swagger需要做其他相關的配置,具體請查看參考文獻
    3. swagger對restful風格的api支持的比較好,非restful風格的api支持的不是很好,對於非restful風格的api或者其他語言(非java語言)可以采用 http://editor.swagger 編輯器來收工完成相關的API編寫
    4. SpringBoot 配置SwaggerUI 訪問404的小坑。

      在學習SpringBoot構建Restful API的時候遇到了一個小坑,配置Swagger UI的時候無法訪問。

      首先在自己的pom文件中加入Swagger的依賴,如下所示:

      ?
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      <dependency>
             <groupId>io.springfox</groupId>
             <artifactId>springfox-swagger-ui</artifactId>
             <version> 2.2 . 2 </version>
           </dependency>
       
           <dependency>
             <groupId>io.springfox</groupId>
             <artifactId>springfox-swagger2</artifactId>
             <version> 2.2 . 2 </version>
      </dependency>

      然后在新建一個SwaggerConfig類:

      ?
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      Configuration
      @EnableSwagger2
      public class SwaggerConfig {
         @Bean
         public Docket createRestApi() {
           return new Docket(DocumentationType.SWAGGER_2)
               .apiInfo(apiInfo())
               .select()
               .apis(RequestHandlerSelectors.basePackage( "com.nightowl" ))
               .paths(PathSelectors.any())
               .build();
         }
         private ApiInfo apiInfo() {
           return new ApiInfoBuilder()
               .title( "NightOwl RESTful APIs" )
               .description( "關注我 http://hwangfantasy.github.io/" )
               .termsOfServiceUrl( "http://hwangfantasy.github.io/" )
               .contact( "顏藝學長" )
               .version( "1.0" )
               .build();
         }
      }

      最后在自己的Controller中加上一系列的API注解即可,其實不需要加上API注解也可以正常使用。 
      最后在localhost:8080/swagger-ui.html 訪問即可看到swagger頁面了。

      但是關鍵來了,我第一次按照這樣的方法配置卻提示如下錯誤:

      ?
      1
      2
      3
      4
      5
      6
      7
      Whitelabel Error Page
       
      This application has no explicit mapping for /error, so you are seeing this as a fallback.
       
      Thu Nov 24 19 : 57 : 13 CST 2016
      There was an unexpected error (type=Not Found, status= 404 ).
      No message available

      但是我新建一個項目重新配置卻沒有任何問題,於是想到自己的項目中肯定有哪些配置與swagger沖突了, 
      最后發現在 application.properties 中把

      ?
      1
      spring.resources. static -locations=classpath:/ static /

      這一行注釋掉即可訪問了。

      以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

       

      原文鏈接:http://blog.csdn.net/hwangfantasy/article/details/66542602

參考文獻

  1. swagger-2-documentation-for-spring-rest-api


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM