Set context path in a Spring Boot application
This post will discuss how to set a context path in a Spring Boot application.
By default, Spring Boot serves content on the root context path (/). We can set the context path of the Spring Boot application in a properties file called application, which is available in two formats – .properties and .yml.
1. Property file
In Spring Boot, we can set the context path in application.properties, as shown in the following example:
|
1 |
server.contextPath=/context-path |
Note that with the release of Spring Boot 2.0.0, the context path property has been changed to the following:
|
1 |
server.servlet.contextPath=/context-path |
2. YAML file
We can also store external properties in a YAML file, application.yml, which is a superset of JSON. The preceding example corresponds to the following YAML file:
|
1 2 |
server: contextPath: /context-path |
For Spring Boot 2.0.0 and above:
|
1 2 3 |
server: servlet: contextPath: /context-path |
Note that both application.properties and application.yml can be placed in four pre-determined locations:
- root of the classpath (
src\main\resources) - current directory
- package
/configin classpath /configsubdirectory of the current directory
We can also use Java System Property, OS environment variables, and command-line arguments to set the Spring Boot application context path.
1. Java System property
We can set server.servlet.context-path as System property inside the main method, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 |
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { System.setProperty("server.servlet.context-path", "/springbootapp"); SpringApplication.run(Application.class, args); } } |
2. Define OS environment variable
We can also use SERVER_SERVLET_CONTEXT_PATH as an OS environment variable in Unix/windows. This works since Spring Boot uses some relaxed rules for binding Environment properties to @ConfigurationProperties beans.
1. Windows
|
1 |
> SET SERVER_SERVLET_CONTEXT_PATH=/springbootapp |
2. UNIX
|
1 |
$ export SERVER_SERVLET_CONTEXT_PATH=/springbootapp |
We can also configure the context path programmatically:
1. Before Spring Boot 2, we can use the EmbeddedServletContainerCustomizer interface.
|
1 2 3 4 5 6 7 8 9 10 11 |
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer; import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer; @Component public class CustomContainer implements EmbeddedServletContainerCustomizer { @Override public void customize(ConfigurableEmbeddedServletContainer container) { container.setContextPath("/springbootapp"); } } |
2. With Spring Boot 2, the EmbeddedServletContainerCustomizer interface is replaced by WebServerFactoryCustomizer,
|
1 2 3 4 5 6 7 8 9 10 |
import org.springframework.boot.web.server.WebServerFactoryCustomizer; import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory; public class ServletConfig implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> { public void customize(ConfigurableServletWebServerFactory factory) { factory.setContextPath("/springbootapp"); } } |
That’s all about setting the context path in a Spring Boot application.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)