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

avatar
6 Comment threads
0 Thread replies
1 Followers
 
Most reacted comment
Hottest comment thread
6 Comment authors
keshav guptaGabriel AndradePankaj ShindeShahid AkhtarSanthosh Rongala Recent comment authors
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.

mtbadi39
Guest
mtbadi39

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

keshav gupta
Guest
keshav gupta

how to know the existing URL in existing Spring -boot application?
like in the old WebLogic world we could open the WebLogic console and find it…

Gabriel Andrade
Guest
Gabriel Andrade

Can you help me find out how to change the context path on a spring boot application running on external tomcat?
Seems that application.properties is being ignored.
One other thing is that I cant deploy more than one WAR in the same tomcat, it gives me the following error:

org.springframework.jmx.export.UnableToRegisterMBeanException: Unable to register MBean [HikariDataSource
(HikariPool-2)] with key ‘dataSource’; nested exception is javax.management.InstanceAlreadyExistsException:
com.zaxxer.hikari:name=dataSource,type=HikariDataSource

Thanks in advance.

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?