Spring Boot – How to change Context Path

In Spring Boot, to change the context path, update server.contextPath properties. The following examples update the context path from / to /mkyong or http://localhost:8080/mkyong

Note
By default, the context path is “/”.

P.S Tested with Spring Boot 1.4.2.RELEASE

1. Properties & Yaml

1.1 Update via a properties file.

/src/main/resources/application.properties

server.port=8080
server.contextPath=/mkyong

1.2 Update via a yaml file.

/src/main/resources/application.yml

server:
  port: 8080
  contextPath: /mkyong

2. EmbeddedServletContainerCustomizer

Update via code, this overrides properties and yaml settings.

CustomContainer.java

package com.mkyong;

import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.stereotype.Component;

@Component
public class CustomContainer implements EmbeddedServletContainerCustomizer {

	@Override
	public void customize(ConfigurableEmbeddedServletContainer container) {

		container.setPort(8080);
		container.setContextPath("/mkyong");

	}

}

3. Command Line

Update the context path by passing the system properties directly.

Terminal

java -jar -Dserver.contextPath=/mkyong spring-boot-example-1.0.jar

References

  1. Spring Boot – Embedded servlet containers
  2. Spring Boot – Externalized Configuration
  3. Spring Boot – How to change Tomcat port

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
Shahid Akhtar
Guest
Shahid Akhtar
Hi Mkyong, EmbeddedServletContainerCustomizer is used to make changes in embedded tomcat and it does not work with standalone tomcat. I tried to change contextPath in both using application.properties file as well as using above mentioned code snippet but it does not work with standalone tomcat. Would you please let me know what changes i have to make so that contextPath can be set for standalone via application.properties or some otherway? Thanks, Shahid Akhtar.
Pankaj Shinde
Guest
Pankaj Shinde
Hi, In spring boot web application, the user is allowed to upload the profile picture. This file gets saved in /resource/user-images folder. When user opens the profile page, the image is shown using the url http://servername:8080/myapp/resource/user-images/.png. This functionality works fine when I am launching application from IDE. I deployed the .war file on Ubuntu linux – I am starting the spring boot application using the embedded tomcat. This functionality stopped working. The image files are getting saved in the folder where .war file is kept but the images are not accessible using the URL – http://servername:8080/myapp/resource/user-images/.png. How to set the… Read more »
Santhosh Rongala
Guest
Santhosh Rongala

how to added jndi URL in Spring Boot? i want configure ehcahe.xml file in tomcat ? but using spring boot where i have to configure ? could you please help me on that? i have some properties files out side the application how to load those files?

mtbadi39
Guest
mtbadi39

Hi,
How to change Spring Security related configuration according to the new Context Path.
Thanks.