在這篇文章中,我們將探索如何在 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中文網其他相關文章!

類加載器通過統一的類文件格式、動態加載、雙親委派模型和平台無關的字節碼,確保Java程序在不同平台上的一致性和兼容性,實現平台獨立性。

Java編譯器生成的代碼是平台無關的,但最終執行的代碼是平台特定的。 1.Java源代碼編譯成平台無關的字節碼。 2.JVM將字節碼轉換為特定平台的機器碼,確保跨平台運行但性能可能不同。

多線程在現代編程中重要,因為它能提高程序的響應性和資源利用率,並處理複雜的並發任務。 JVM通過線程映射、調度機制和同步鎖機制,在不同操作系統上確保多線程的一致性和高效性。

Java的平台獨立性是指編寫的代碼可以在任何安裝了JVM的平台上運行,無需修改。 1)Java源代碼編譯成字節碼,2)字節碼由JVM解釋執行,3)JVM提供內存管理和垃圾回收功能,確保程序在不同操作系統上運行。

Javaapplicationscanindeedencounterplatform-specificissuesdespitetheJVM'sabstraction.Reasonsinclude:1)Nativecodeandlibraries,2)Operatingsystemdifferences,3)JVMimplementationvariations,and4)Hardwaredependencies.Tomitigatethese,developersshould:1)Conduc

云计算显著提升了Java的平台独立性。1)Java代码编译为字节码,由JVM在不同操作系统上执行,确保跨平台运行。2)使用Docker和Kubernetes部署Java应用,提高可移植性和可扩展性。

Java'splatformindependenceallowsdeveloperstowritecodeonceandrunitonanydeviceorOSwithaJVM.Thisisachievedthroughcompilingtobytecode,whichtheJVMinterpretsorcompilesatruntime.ThisfeaturehassignificantlyboostedJava'sadoptionduetocross-platformdeployment,s

容器化技術如Docker增強而非替代Java的平台獨立性。 1)確保跨環境的一致性,2)管理依賴性,包括特定JVM版本,3)簡化部署過程,使Java應用更具適應性和易管理性。


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3 Linux新版
SublimeText3 Linux最新版