Home  >  Article  >  Java  >  How to Set a Context Path in a Spring Boot Application?

How to Set a Context Path in a Spring Boot Application?

DDD
DDDOriginal
2024-10-26 01:52:27673browse

 How to Set a Context Path in a Spring Boot Application?

Add Context Path to 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn