」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > 以下是一些適合您文章內容的基於問題的標題: * 如何為 Spring Boot 應用程式配置上下文路徑? * 如何使用自訂 Con 存取我的 Spring Boot 應用程式

以下是一些適合您文章內容的基於問題的標題: * 如何為 Spring Boot 應用程式配置上下文路徑? * 如何使用自訂 Con 存取我的 Spring Boot 應用程式

發佈於2024-11-08
瀏覽:128

Here are a few question-based titles that fit the content of your article:

* How to Configure a Context Path for Your Spring Boot Application?
* How Can I Access My Spring Boot App Using a Custom Context Path?
* Want to Control the URL Path of Your Sprin

如何為Spring Boot 應用程式添加上下文路徑

Spring Boot 提供了一種簡單的方法來設定應用程式的上下文根,允許它透過localhost:port/{app_name} 存取。操作方法如下:

  1. 使用應用程式屬性:

    在src/main/resources 目錄中建立一個application.properties 檔案並新增以下屬性:

    server.contextPath=/mainstay
    server.port=12378
  2. 刪除自訂Servlet 容器配置:

    如果您的應用程式中有自訂Servlet 容器配置,例如EmbeddedServletContainerFactory,刪除它。

  3. 使用EmbeddedServletContainerCustomizer:

    如果需要對servlet容器進行後處理,則實作EmbeddedServletContainerCustomizer介面並將其新增至您的設定中。例如新增錯誤頁面:

    @Bean
    public EmbeddedServletContainerCustomizer errorPageCustomizer() {
        return factory -> {
            ErrorPage notFoundPage = new ErrorPage(HttpStatus.NOT_FOUND, "/notfound.html");
            ErrorPage forbiddenPage = new ErrorPage(HttpStatus.FORBIDDEN, "/forbidden.html");
            factory.setErrorPages(Arrays.asList(notFoundPage, forbiddenPage));
        };
    }
  4. 重寫屬性:

    您可以使用外部屬性檔案或 JVM 參數來重寫 application.properties 中設定的預設屬性。

此設定會將上下文路徑設為 /mainstay 並使您的應用程式在連接埠 12378 上運行。然後您的應用程式將可以透過 localhost:12378/mainstay 存取。

最新教學 更多>

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3