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

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=/favtuts

1.2 Update via a yaml file.

/src/main/resources/application.yml

server:
  port: 8080
  contextPath: /favtuts

2. EmbeddedServletContainerCustomizer

Update via code, this overrides properties and yaml settings.

CustomContainer.java

package com.favtuts;

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("/favtuts");

	}

}

3. Command Line

Update the context path by passing the system properties directly.

Terminal

java -jar -Dserver.contextPath=/favtuts 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

Leave a Reply

Your email address will not be published. Required fields are marked *