springdoc-openapi主頁翻譯

原文地址

springdoc-openapi是一個基于spring-bootOpenAPI 3類庫。

簡介

springdoc-openapi java庫幫助基于spring boot開發(fā)的項目自動生成API文檔。
springdoc-openapi 通過在運行時檢查應(yīng)用程序,根據(jù)spring配置、類結(jié)構(gòu)和各種注解推斷API語義。

自動生成JSON/YAML和HTML格式的API文檔。這個文檔的所有內(nèi)容都可以使用swagger-api注解來編寫。

本庫支持:

  • OpenAPI 3
  • Spring-boot (v1 and v2)
  • JSR-303, specifically for @NotNull, @Min, @Max, and @Size.
  • Swagger-ui
  • OAuth 2

以下是本庫的介紹視頻(youtube視頻):

Watch the video

Code

這是一個社區(qū)驅(qū)動的項目, 并不是Spring官方維護的項目。

入門

overview

集成Spring-boot和本類庫(包含界面))

  • 自動將swagger-ui部署到spring-boot應(yīng)用程序中。

  • 文檔將以 HTML 格式提供,使用官方的 swagger-ui jars

  • 通過http://server:port/context-path/swagger-ui.html打開Swagger UI頁面,而OpenAPI的描述將在以下json格式的url中提供:http://server:port/context-path/v3/api-docs。

    • server: 服務(wù)器名稱或IP
    • port: 服務(wù)器端口
    • context-path: 應(yīng)用程序的上下文路徑
  • 文檔也可以以yaml格式提供,路徑如下:http://server:port/context-path/v3/api-docs.yml。

  • 將庫添加到你的項目依賴列表中(不需要額外的配置)。

    <dependency>
       <groupId>org.springdoc</groupId>
       <artifactId>springdoc-openapi-ui</artifactId>
       <version>1.4.8</version>
    </dependency>
    
  • 這一步是可選的。對于HTML格式的swagger文檔的自定義路徑,在你的spring-boot配置文件中添加一個自定義springdoc屬性。

     # swagger-ui custom path
     springdoc.swagger-ui.path: /swagger-ui.html
    

    json格式的文檔將在以下網(wǎng)址上提供:http://server:port/context-path/v3/api-docs。
    服務(wù)器。服務(wù)器名稱或IP
    端口。服務(wù)器端口
    的上下文路徑。應(yīng)用程序的上下文路徑
    文檔也將以yaml格式提供,路徑如下:/v3/api-docs.yml。
    將該庫添加到你的項目依賴列表中。(不需要額外的配置)

集成Spring-boot和本類庫(不含界面))

  • 文檔可以通過http://server:port/context-path/v3/api-docs訪問(json格式)

    • server: 服務(wù)器名稱或IP
    • port: 服務(wù)器端口
    • context-path: 應(yīng)用程序的上下文路徑
  • 文檔也可以以yaml格式提供,路徑如下:http://server:port/context-path/v3/api-docs.yml

  • 將庫添加到你的項目依賴列表中(不需要額外的配置)。

    <dependency>
       <groupId>org.springdoc</groupId>
       <artifactId>springdoc-openapi-webmvc-core</artifactId>
       <version>1.4.8</version>
    </dependency>
    
  • 這一步是可選的。對于HTML格式的swagger文檔的自定義路徑,在你的spring-boot配置文件中添加一個自定義springdoc屬性。

     # swagger-ui custom path
     springdoc.api-docs.path: /api-docs
    

WildFly 用戶需要再加上此依賴才能正常使用swagger-ui:

<dependency>
  <groupId>org.webjars</groupId>
  <artifactId>webjars-locator-jboss-vfs</artifactId>
  <version>0.1.0</version>
</dependency>

其他設(shè)置

添加API信息和安全文檔

該庫使用spring-boot應(yīng)用自動配置機制來掃描spring beans中的以下注解: OpenAPIDefinition和Info。
這些注解定義了API的以下信息: 標題、版本、許可、安全、服務(wù)器、標簽、安全和外部文檔。
為了獲得更好的文檔生成性能,
請在spring管理的Bean中聲明@OpenAPIDefinition@SecurityScheme注解。

使用@ControllerAdvice進行REST錯誤處理

要自動生成文檔,請確保所有方法都使用注解聲明HTTP代碼響應(yīng)。@ResponseStatus

禁用 springdoc-openapi 端點。

為了禁用springdoc-openapi端點(默認情況下是/v3/api-docs),使用以下屬性。

# Disabling the /v3/api-docs enpoint
springdoc.api-docs.enabled: false

禁用 swagger-ui

使用以下配置可以禁用swagger-ui界面:

# Disabling the swagger-ui
springdoc.swagger-ui.enabled: false

Swagger-ui 配置

The library supports the swagger-ui official properties:

https://swagger.io/docs/open-source-tools/swagger-ui/usage/configuration/

你需要將 swagger-ui 屬性聲明為 spring-boot 屬性。所有這些屬性都應(yīng)該用以下前綴來聲明:springdoc.swagger-ui。

選擇要包含在文檔中的Rest Controllers

除了來自 swagger-annotations 的 @Hidden 注釋,它還可以使用包或路徑配置來限制生成的 OpenAPI 描述。

對于要包含的包列表,使用以下屬性。

# Packages to include
springdoc.packagesToScan: com.package1, com.package2

For the list of paths to include, use the following property:
如果要包含指定的路徑, 使用以下配置屬性:

# Paths to include
springdoc.pathsToMatch=/v1, /api/balance/**

Spring-webflux 支持

   <dependency>
      <groupId>org.springdoc</groupId>
      <artifactId>springdoc-openapi-webflux-ui</artifactId>
      <version>1.4.8</version>
   </dependency>

Spring-webflux/WebMvc.fn 函數(shù)式編程支持

Spring Hateoas 支持

HATEOAS是Hypertext As The Engine Of Application State的縮寫。在Richardson Maturity Model中, 它是REST的最高級形態(tài)。

Richardson 提出的 REST 成熟度模型把 REST 服務(wù)按照成熟度劃分成 4 個層次:

  1. 第一個層次(Level 0)的 Web 服務(wù)只是使用 HTTP 作為傳輸方式,實際上只是遠程方法調(diào)用(RPC)的一種具體形式。SOAP 和 XML-RPC 都屬于此類。
  2. 第二個層次(Level 1)的 Web 服務(wù)引入了資源的概念。每個資源有對應(yīng)的標識符和表達。
  3. 第三個層次(Level 2)的 Web 服務(wù)使用不同的 HTTP 方法來進行不同的操作,并且使用 HTTP 狀態(tài)碼來表示不同的結(jié)果。如 HTTP GET 方法來獲取資源,HTTP DELETE 方法來刪除資源。
  4. 第四個層次(Level 3)的 Web 服務(wù)使用 HATEOAS。在資源的表達中包含了鏈接信息??蛻舳丝梢愿鶕?jù)鏈接來發(fā)現(xiàn)可以執(zhí)行的動作。

該模型將REST劃作了由低到高四個等級,等級越高,RESTful就越成熟。

通過加入以下依賴, 可以支持Spring Hateoas:

   <dependency>
      <groupId>org.springdoc</groupId>
      <artifactId>springdoc-openapi-hateoas</artifactId>
      <version>1.4.8</version>
   </dependency>

Spring Data Rest 支持

   <dependency>
      <groupId>org.springdoc</groupId>
      <artifactId>springdoc-openapi-data-rest</artifactId>
      <version>1.4.8</version>
   </dependency>

Spring security 支持

   <dependency>
      <groupId>org.springdoc</groupId>
      <artifactId>springdoc-openapi-security</artifactId>
      <version>1.4.8</version>
   </dependency>

Kotlin 支持

   <dependency>
      <groupId>org.springdoc</groupId>
      <artifactId>springdoc-openapi-kotlin</artifactId>
      <version>1.4.8</version>
   </dependency>

Groovy 支持

   <dependency>
      <groupId>org.springdoc</groupId>
      <artifactId>springdoc-openapi-groovy</artifactId>
      <version>1.4.8</version>
   </dependency>

MAVEN插件介紹 springdoc-openapi-maven-plugin

springdoc-openapi-maven-plugin的目的是在構(gòu)建期間生成json和yaml OpenAPI描述。
該插件在集成測試階段工作,并生成OpenAPI描述。
該插件與spring-boot-maven插件一起工作。

你可以在集成測試階段使用maven命令測試它。

mvn verify -Dspring.application.admin.enabled=true

為了使用這個功能,您需要在您的pom.xml的plugins部分添加插件聲明。

<xxx>
    <plugin>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-maven-plugin</artifactId>
     <version>2.3.0.RELEASE</version>
     <executions>
      <execution>
       <id>pre-integration-test</id>
       <goals>
        <goal>start</goal>
       </goals>
      </execution>
      <execution>
       <id>post-integration-test</id>
       <goals>
        <goal>stop</goal>
       </goals>
      </execution>
     </executions>
    </plugin>
    
    <plugin>
     <groupId>org.springdoc</groupId>
     <artifactId>springdoc-openapi-maven-plugin</artifactId>
     <version>1.0</version>
     <executions>
      <execution>
       <id>integration-test</id>
       <goals>
        <goal>generate</goal>
       </goals>
      </execution>
     </executions>
    </plugin>
</xxx>

該插件的文檔說明網(wǎng)址:

https://github.com/springdoc/springdoc-openapi-maven-plugin

Gradle插件介紹 springdoc-openapi-gradle-plugin

DEMO程序列表

Demo Spring Boot 2 Web MVC with OpenAPI 3.
Demo Spring Boot 2 WebFlux with OpenAPI 3.
Demo Spring Boot 1 Web MVC with OpenAPI 3.
Demo Spring Boot 2 WebFlux with Functional endpoints OpenAPI 3.
Demo Spring Boot 2 and Spring Hateoas with OpenAPI 3.

Branching

DEMO程序源碼倉庫

https://github.com/springdoc/springdoc-openapi-demos.git

依賴倉庫

本庫發(fā)布在maven中央倉庫, maven坐標可以通過以下網(wǎng)址查到:

正式版本:

https://oss.sonatype.org/content/groups/public/org/springdoc/.

快照版本:

https://oss.sonatype.org/content/repositories/snapshots/org/springdoc/.
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容