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

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

Barbara Streisand
Barbara Streisand原創
2024-10-28 03:21:31976瀏覽

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 檔案並新增下列屬性:

    <code class="properties">server.contextPath=/mainstay
    server.port=12378</code>
  2. 刪除自訂Servlet 容器設定:

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

  3. 使用EmbeddedServletContainerCustomizer:

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

    <code class="java">@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));
        };
    }</code>
  4. 覆蓋屬性:

    您可以透過下列方式覆寫application.properties 中設定的預設屬性:使用外部屬性檔或JVM 參數。

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

以上是以下是一些適合您文章內容的基於問題的標題: * 如何為 Spring Boot 應用程式配置上下文路徑? * 如何使用自訂 Con 存取我的 Spring Boot 應用程式的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn