Spring Boot Change Context Path

By Arvind Rai, December 04, 2023
On this page, we will learn to change Spring Boot context path. There are many ways to change default context path.
1. In web application, the default context path is ("/"). We can change context path by configuring property server.servlet.context-path for Spring Boot 2.x and server.contextPath for Spring 1.x in property file as well as in command line as arguments with java command.
2. We can also configure SERVER_SERVLET_CONTEXT_PATH for Spring Boot 2.x and SERVER_CONTEXT_PATH for Spring Boot 1.x as OS environment variable as well as in eclipse environment variable to change context path or context root.
3. Spring boot also provides API to change context path programmatically.

In our example, we will change context path as /spring-boot-app and server port as 8080. While configuring context path name, we should take care to add ("/") as prefix with context path name.

1. Using Property File (.properties/.yml)

We can change context root and server port using property file and yml file. Suppose we want to create context root as /spring-boot-app and server port as 8585. We will do as follows.
a. Using property file src\main\resources\application.properties
For Spring Boot 2.x, use server.servlet.context-path
server.servlet.context-path = /spring-boot-app
server.port = 8585 
For Spring Boot 1.x, use server.contextPath
server.contextPath = /spring-boot-app
server.port = 8585 

b. Using yml file src\main\resources\application.yml
Using Spring Boot 2.x
server:
  servlet:
    context-path: /spring-boot-app
  port: 8585 
Using Spring Boot 1.x
server:
  contextPath: /spring-boot-app
  port: 8585 

2. Using java Command

We can change spring boot web application context root using java command. Build the project and let's say we get executable JAR named my-app.jar. Suppose we want to change default context root from ("/") to /spring-boot-app with port 8585.
1. For Spring Boot 2.x
Using --server.servlet.context-path
java -jar my-app.jar --server.servlet.context-path=/spring-boot-app --server.port=8585 
Using -Dserver.servlet.context-path
java -jar -Dserver.servlet.context-path=/spring-boot-app -Dserver.port=8585 my-app.jar 

2. For Spring Boot 1.x
Using --server.contextPath
java -jar my-app.jar --server.contextPath=/spring-boot-app --server.port=8585 
Using -Dserver.contextPath
java -jar -Dserver.contextPath=/spring-boot-app -Dserver.port=8585 my-app.jar 

3. Change Context Path Programmatically

SpringApplication has a method as setDefaultProperties() that is used to change spring boot default properties. Suppose we want to change context path and default port then we need to create a Map and put server.servlet.context-path key with value of desired context path name using prefix ("/"). This will work in Spring Boot 2.x. For Spring Boot 1.x, use server.contextPath to change context path. To change server port we need to put server.port key with desired port value. Find the example for Spring Boot 2.x.
MyApplication.java
@SpringBootApplication
public class MyApplication {
	public static void main(String[] args) {
		SpringApplication application = new SpringApplication(MyApplication.class);
		Map<String, Object> map = new HashMap<>();
		map.put("server.servlet.context-path", "/spring-boot-app");
		map.put("server.port", "8585");
		application.setDefaultProperties(map);
		application.run(args);
        }       
} 

4. Using EmbeddedServletContainerCustomizer

We can change embedded servlet container default settings registering a bean that implements EmbeddedServletContainerCustomizer interface. We need to override its customize() method. Find the example.
ContainerCustomizerBean.java
@Component
public class ContainerCustomizerBean implements EmbeddedServletContainerCustomizer {
	@Override
	public void customize(ConfigurableEmbeddedServletContainer container) {
		container.setContextPath("/spring-boot-app");
		container.setPort(8585);
	}
} 

5. Using OS Environment Variable

We can change spring boot context path and default server port by setting SERVER_CONTEXT_PATH (for Spring Boot 1.x) and SERVER_PORT respectively as OS (such as Windows and Linux) environment variables. In Spring Boot 2.x, use SERVER_SERVLET_CONTEXT_PATH variable to change context path. I am using Windows 7. Find the steps to configure environment variables for Spring Boot 1.x.
Step 1: Right click on Computer icon then go to Properties -> Advanced System Settings -> Environment Variables and set variables as follows.
SERVER_CONTEXT_PATH = /spring-boot-app
SERVER_PORT = 8585 
Find the print screen.
Spring Boot Change Context Path
Step 2: Open the command prompt and build the project. Suppose we get an executable JAR as my-app.jar then run it using java command as follows.
java -jar my-app.jar 
If we want to run spring boot application from eclipse console, first restart the eclipse and then run the application.

6. Using Eclipse Run Configurations

We can change spring boot default settings in eclipse by configuring Environment variables in run configurations.
Step-1: Right click on the class and go to Run As -> Run Configurations. For Spring Boot 1.x, use SERVER_CONTEXT_PATH and for Spring Boot 2.x, use SERVER_SERVLET_CONTEXT_PATH.
Step-2: Click on the Environment tab and configure context path and server port as follows.
SERVER_CONTEXT_PATH = /spring-boot-app
SERVER_PORT = 8585 
Find the print screen of eclipse.
Spring Boot Change Context Path
Step-3: Run the application from eclipse console. Server will start with context path spring-boot-app and 8585 port. Find the console output.
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.2.RELEASE)

2017-03-21 15:17:36.931  INFO 2212 --- [           main] com.concretepage.MyApplication           : Starting MyApplication on Renu-PC with PID 2212 (F:\arvind\PROJECT\mars\spring-boot\spring-boot-demo\target\classes started by Renu in F:\arvind\PROJECT\mars\spring-boot\spring-boot-demo)
2017-03-21 15:17:36.936  INFO 2212 --- [           main] com.concretepage.MyApplication           : No active profile set, falling back to default profiles: default
2017-03-21 15:17:37.043  INFO 2212 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@57f23557: startup date [Tue Mar 21 15:17:37 IST 2017]; root of context hierarchy
2017-03-21 15:17:39.049  INFO 2212 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8585 (http)
2017-03-21 15:17:39.071  INFO 2212 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2017-03-21 15:17:39.073  INFO 2212 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.11
2017-03-21 15:17:39.303  INFO 2212 --- [ost-startStop-1] o.a.c.c.C.[.[.[/spring-boot-app]         : Initializing Spring embedded WebApplicationContext 

7. Reference

Spring Boot Reference Guide
POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us