搜尋
首頁Javajava教程SpringBoot Web服務-部分初始配置

SpringBoot Web Service - Part  Initial Configuration

在這篇文章中,我們將探索如何在 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 應用程式的幾個關鍵領域:

  1. 應用程式屬性:

name:定義應用程式的名稱,這裡設定為 book-catalog。

版本:引用可能在建置過程中填入的佔位符,以指定應用程式的版本。

  1. MVC 配置:

problemdetails.enabled:在異常回應正文中啟用詳細問題報告。

  1. 管理端點:

endpoints.web.exposure.include: '*':** 公開所有執行器端點以用於監視和管理目的。

info.git.mode: full:在 /info 端點提供詳細的 Git 資訊。

  1. 伺服器設定:

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中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
Java平台獨立性:OS之間的差異Java平台獨立性:OS之間的差異May 16, 2025 am 12:18 AM

Java在不同操作系統上的表現存在細微差異。 1)JVM實現不同,如HotSpot、OpenJDK,影響性能和垃圾回收。 2)文件系統結構和路徑分隔符不同,需使用Java標準庫處理。 3)網絡協議實現差異影響網絡性能。 4)GUI組件外觀和行為在不同系統上有別。通過使用標準庫和虛擬機測試,可減少這些差異的影響,確保Java程序穩定運行。

Java的最佳功能:從面向對象的編程到安全性Java的最佳功能:從面向對象的編程到安全性May 16, 2025 am 12:15 AM

javaoffersrobustobject-IentiendedProgrammming(OOP)和Top-Notchsecurityfeatures.1)OopinjavainCludesClasses,對象,繼承,多態性,和列出,andeclingfleximaintainablesys.ss.2)SecurityFeateTuersLudEtersludEterMachine(

JavaScript與Java的最佳功能JavaScript與Java的最佳功能May 16, 2025 am 12:13 AM

JavaScriptandJavahavedistinctstrengths:JavaScriptexcelsindynamictypingandasynchronousprogramming,whileJavaisrobustwithstrongOOPandtyping.1)JavaScript'sdynamicnatureallowsforrapiddevelopmentandprototyping,withasync/awaitfornon-blockingI/O.2)Java'sOOPf

Java平台獨立性:收益,限制和實施Java平台獨立性:收益,限制和實施May 16, 2025 am 12:12 AM

JAVAACHIEVESPLATFORMINDEPENTENCETHROUGHJAVAVIRTAILMACHINE(JVM)和BYTECODE.1)THEJVMINTERPRETSBBYTECODE,允許theingthesmecodetorunonanyanyanyanyplatformwithajvm.2)

Java:真實詞的平台獨立性Java:真實詞的平台獨立性May 16, 2025 am 12:07 AM

java'splatformendependecemeansapplicationscanrunonanyplatformwithajvm,使“ Writeonce,runanywhere”。

JVM性能與其他語言JVM性能與其他語言May 14, 2025 am 12:16 AM

JVM'SperformanceIsCompetitiveWithOtherRuntimes,operingabalanceOfspeed,安全性和生產性。 1)JVMUSESJITCOMPILATIONFORDYNAMICOPTIMIZAIZATIONS.2)c提供NativePernativePerformanceButlanceButlactsjvm'ssafetyFeatures.3)

Java平台獨立性:使用示例Java平台獨立性:使用示例May 14, 2025 am 12:14 AM

JavaachievesPlatFormIndependencEthroughTheJavavIrtualMachine(JVM),允許CodeTorunonAnyPlatFormWithAjvm.1)codeisscompiledIntobytecode,notmachine-specificodificcode.2)bytecodeisisteredbytheybytheybytheybythejvm,enablingcross-platerssectectectectectross-eenablingcrossectectectectectection.2)

JVM架構:深入研究Java虛擬機JVM架構:深入研究Java虛擬機May 14, 2025 am 12:12 AM

TheJVMisanabstractcomputingmachinecrucialforrunningJavaprogramsduetoitsplatform-independentarchitecture.Itincludes:1)ClassLoaderforloadingclasses,2)RuntimeDataAreafordatastorage,3)ExecutionEnginewithInterpreter,JITCompiler,andGarbageCollectorforbytec

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

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

熱門文章

北端:融合系統,解釋
4 週前By尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆樹的耳語 - 如何解鎖抓鉤
4 週前By尊渡假赌尊渡假赌尊渡假赌
<🎜>掩蓋:探險33-如何獲得完美的色度催化劑
2 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。