Home >Java >javaTutorial >How to Set a Context Path in a Spring Boot Application?
Q: I'm trying to set the context root programmatically in a Spring Boot application, but it's not appending to the request mapping. What am I missing?
A: You're attempting to create a custom solution, but Spring Boot already supports this feature. Create an application.properties file in src/main/resources and add the following properties:
server.contextPath=/mainstay server.port=12378
For Spring Boot 2.0:
server.servlet.context-path=/mainstay
Remove the custom servlet container configuration, or use an EmbeddedServletContainerCustomizer to apply post-processing.
The application.properties values serve as defaults, which you can override using another application.properties file or JVM parameters (-Dserver.port=6666).
The ServerProperties class implements EmbeddedServletContainerCustomizer, with a default context path of "". In your code, you're directly setting the context path on TomcatEmbeddedServletContainerFactory. However, ServerProperties will override it with "" during processing.
The above is the detailed content of How to Set a Context Path in a Spring Boot Application?. For more information, please follow other related articles on the PHP Chinese website!