The Spring Boot change Context Path shows you some ways to configure Context Path in Spring Boot web application. In Spring Boot, the Tomcat default is context path ‘/’. We can change this context path with command line, or in the java source code, or update properties file.
Other interesting posts you may like
Let’s begin:
Update the Properties & Yaml file
Properties file: create your properties file and place it in the classpath directory, for example.
/src/main/resources/application.properties
1 2 3 4 |
# change tomcat port 8080 to 80 # change context path '/' to '/javabycode' server.port=80 server.contextPath=/javabycode |
YAML file: create your yaml file and place it in the classpath directory, for example.
/src/main/resources/application.yml
1 2 3 |
server: port: 80 contextPath: /javabycode |
Update via Java code
We create a component which implements EmbeddedServletContainerCustomizer and this overrides properties and yaml settings.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com.javabycode; import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer; import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer; import org.springframework.stereotype.Component; @Component public class MyCustomContainer implements EmbeddedServletContainerCustomizer { @Override public void customize(ConfigurableEmbeddedServletContainer container) { container.setPort(80); container.setContextPath("/javabycode"); } } |
Using Command Line
Change the Tomcat default port by passing the system properties directly. For example, we have a jar file named spring-boot-change-contextPath-1.0.jar. We run the below command to change port.
1 |
java -jar -Dserver.contextPath=/javabycode spring-boot-change-contextPath-1.0.jar |
That’s all on the Spring Boot change Context Path.
References
Spring Boot – Embedded servlet containers
Spring Boot – Externalized Configuration
Spring Boot change Tomcat port command line