如何為Spring Boot 應用程式新增上下文路徑
Spring Boot 提供了一個簡單的方法來設定應用程式的上下文根,允許它透過localhost:port/{app_name} 存取。操作方法如下:
使用應用程式屬性:
在src/main/resources 目錄中建立一個application.properties 檔案並新增下列屬性:
<code class="properties">server.contextPath=/mainstay server.port=12378</code>
刪除自訂Servlet 容器設定:
如果您的應用程式中有自訂Servlet 容器配置,例如作為EmbeddedServletContainerFactory,將其刪除。
使用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>
覆蓋屬性:
您可以透過下列方式覆寫application.properties 中設定的預設屬性:使用外部屬性檔或JVM 參數。
此設定會將上下文路徑設為 /mainstay 並使您的應用程式在連接埠 12378 上執行。然後您的應用程式將可以透過 localhost:12378 存取/支柱。
以上是以下是一些適合您文章內容的基於問題的標題: * 如何為 Spring Boot 應用程式配置上下文路徑? * 如何使用自訂 Con 存取我的 Spring Boot 應用程式的詳細內容。更多資訊請關注PHP中文網其他相關文章!