Home >Java >javaTutorial >How to modify the default context of Spring Boot (detailed method)
The content of this article is about how to modify the default context of Spring Boot (detailed explanation of the method). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
By default, the service context used by Spring Boot is "/", we can pass"http://localhost:PORT/"
Access the application directly;
But in the production environment, in many cases, we need to change the default context of the application;
Fortunately, Spring Boot is very flexible and simple, and provides functions There are many ways to change the context of the application;
Through the application.properties configuration file, we can configure it very easily Various parameters, changing the context is one of them, the configuration is as follows:
### Default server path ######### server.port=8080 ### Context root path ######## server.contextPath=/home
EmbeddedServletContainerCustomizer interface can be used for customization Configure the parameters related to the built-in Servlet container. Any Bean that implements the EmbeddedServletContainerCustomizer interface will get a callback, which will be executed before the container starts. We can set the port, context, error page, etc.;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer; import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer; import org.springframework.stereotype.Component; @Component public class AppContainerCustomizer implements EmbeddedServletContainerCustomizer { @Override public void customize(ConfigurableEmbeddedServletContainer container) { container.setPort(8080); container.setContextPath("/home"); } }
If our application is packaged in uber, we can use the following startup command to configure the context:
java -jar -Dserver.contextPath=/home spring-boot-demo.jar
The above is the detailed content of How to modify the default context of Spring Boot (detailed method). For more information, please follow other related articles on the PHP Chinese website!