How to Add Context Path to Spring Boot Application
Spring Boot provides an easy way to set the context root for your application, allowing it to be accessed via localhost:port/{app_name}. Here's how to do it:
Use Application Properties:
Create a application.properties file in the src/main/resources directory and add the following properties:
<code class="properties">server.contextPath=/mainstay server.port=12378</code>
Remove Custom Servlet Container Configuration:
If you have a custom servlet container configuration in your application, such as the EmbeddedServletContainerFactory, remove it.
Use EmbeddedServletContainerCustomizer:
If you need to perform post-processing on the servlet container, implement the EmbeddedServletContainerCustomizer interface and add it to your configuration. For example, to add error pages:
<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>
Overriding Properties:
You can override the default properties set in application.properties by using an external properties file or JVM parameters.
This setup will set the context path to /mainstay and have your application run on port 12378. Your application will then be accessible via localhost:12378/mainstay.
The above is the detailed content of 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 Con. For more information, please follow other related articles on the PHP Chinese website!