SpringBoot Web服務-部分初始配置
在這篇文章中,我們將探索如何在 Spring Boot 應用程式中設定 OpenAPI,並新增從根 URL 到 Swagger UI 的便利重定向。此設定將改進您的 API 文件並使其更易於開發人員存取。
OpenAPI Bean 配置
首先,讓我們建立一個設定類別來自訂我們的 OpenAPI 文件:
import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.info.Contact; import io.swagger.v3.oas.models.info.Info; import io.swagger.v3.oas.models.info.License; import org.springframework.boot.info.GitProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class OpenAPIConfig { @Bean public OpenAPI customOpenAPI(GitProperties gitProperties) { return new OpenAPI() .info(new Info() .title("Book Catalog API") .description("REST API for managing a book catalog. Application version: "+ gitProperties.get("build.version")) .version("1.0.0") .contact(new Contact() .name("Book Catalog Team") .email("support@bookcatalog.com") .url("https://github.com/vlaship/book-catalog")) .license(new License() .name("MIT License") .url("https://opensource.org/licenses/MIT")) ); } }
此組態建立一個自訂 OpenAPI bean,其中包含有關您的 API 的基本資訊。您可以透過新增更多詳細資訊(例如聯絡資訊、授權或外部文件)來進一步自訂。
我們可以使用 GitProperties 來提供更多詳細資訊。
根 URL 重新導向控制器
接下來,讓我們建立一個控制器來將使用者從根 URL 重新導向到 Swagger UI:
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @Controller public class OpenApiController { @GetMapping("/") public String index() { return "redirect:swagger-ui.html"; } }
這個簡單的控制器使用 @GetMapping 作為根 URL(“/”)並重定向到 Swagger UI HTML 頁面。
更新 application.yaml
此設定檔通常名為 application.yml,在定義應用程式行為的各個方面起著至關重要的作用。
spring: application: name: book-catalog version: '@project.version@' mvc: problemdetails: enabled: true management: endpoints: web: exposure: include: '*' info: git: mode: full server: port: 8888 servlet: context-path: /${spring.application.name} error: whitelabel: enabled: false
提供的 YAML 設定涵蓋了 Spring Boot 應用程式的幾個關鍵領域:
- 應用程式屬性:
name:定義應用程式的名稱,這裡設定為 book-catalog。
版本:引用可能在建置過程中填入的佔位符,以指定應用程式的版本。
- MVC 配置:
problemdetails.enabled:在異常回應正文中啟用詳細問題報告。
- 管理端點:
endpoints.web.exposure.include: '*':** 公開所有執行器端點以用於監視和管理目的。
info.git.mode: full:在 /info 端點提供詳細的 Git 資訊。
- 伺服器設定:
port:設定伺服器監聽傳入請求的連接埠(預設8080,這裡設定為8888)。
servlet.context-path:定義應用程式的上下文路徑,確保請求正確路由。
error.whitelabel.enabled: false:停用預設的白標籤錯誤頁面,以便在開發過程中提供更多資訊性錯誤訊息。
新增banner.txt
1。建立banner.txt檔案
在 Spring Boot 專案的 src/main/resources 目錄中建立一個名為 Banner.txt 的新檔案。
2。將服務詳細資訊加入banner.txt
您可以將任何文字或 ASCII 藝術添加到此文件中。這是一個例子:
import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.info.Contact; import io.swagger.v3.oas.models.info.Info; import io.swagger.v3.oas.models.info.License; import org.springframework.boot.info.GitProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class OpenAPIConfig { @Bean public OpenAPI customOpenAPI(GitProperties gitProperties) { return new OpenAPI() .info(new Info() .title("Book Catalog API") .description("REST API for managing a book catalog. Application version: "+ gitProperties.get("build.version")) .version("1.0.0") .contact(new Contact() .name("Book Catalog Team") .email("support@bookcatalog.com") .url("https://github.com/vlaship/book-catalog")) .license(new License() .name("MIT License") .url("https://opensource.org/licenses/MIT")) ); } }
這種方法為您的應用程式啟動增添了專業感,並一目了然地提供有價值的資訊。
新增 Dockerfile
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @Controller public class OpenApiController { @GetMapping("/") public String index() { return "redirect:swagger-ui.html"; } }
圖層擷取:為依賴項建立單獨的圖層,提高建置效率並減少影像大小更新。
多階段建置:利用多階段建置流程將建置環境與執行時間環境分開,產生較小、更有效率的最終影像。
輕量級基礎鏡像: 使用像 azul/zulu-openjdk-alpine:21-jre-headless 這樣的最小基礎鏡像來進一步減少鏡像大小。
這種方法可以加快 Docker 容器內 Spring Boot 應用程式的建置速度、縮小映像大小並提高整體效能。
以上是SpringBoot Web服務-部分初始配置的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

公司安全軟件導致部分應用無法正常運行的排查與解決方法許多公司為了保障內部網絡安全,會部署安全軟件。 ...

將姓名轉換為數字以實現排序的解決方案在許多應用場景中,用戶可能需要在群組中進行排序,尤其是在一個用...

在使用IntelliJIDEAUltimate版本啟動Spring...

系統對接中的字段映射處理在進行系統對接時,常常會遇到一個棘手的問題:如何將A系統的接口字段有效地映�...

在使用MyBatis-Plus或其他ORM框架進行數據庫操作時,經常需要根據實體類的屬性名構造查詢條件。如果每次都手動...

Java對象與數組的轉換:深入探討強制類型轉換的風險與正確方法很多Java初學者會遇到將一個對象轉換成數組的�...

Redis緩存方案如何實現產品排行榜列表的需求?在開發過程中,我們常常需要處理排行榜的需求,例如展示一個�...

電商平台SKU和SPU表設計詳解本文將探討電商平台中SKU和SPU的數據庫設計問題,特別是如何處理用戶自定義銷售屬...
