How to Change Spring Boot Context Path

By Yashwant Chavan, Views 92, Last updated on 15-Apr-2019

By default Spring Boot application accessed via root context path ("/") like http://localhost:port/. But in real world application development, you have to specify the context path http://localhost:port/{app_name}.

In this tutorial, we will learn the different ways to configure root context.

tags spring spring-boot

application.properties

You can change Spring Boot application context root using "/src/main/resources/application.properties" file. Just define the property server.servlet.context-path along with context root value.

server.servlet.context-path=/technicalkeeda

Java System Property

We can set the context path using a Java system property System.setProperty("server.servlet.context-path", "/technicalkeeda");.

package com.technicalkeeda.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(basePackages={"com.technicalkeeda"})
public class Application {
	public static void main(String[] args) {
		System.setProperty("server.servlet.context-path", "/technicalkeeda");
		SpringApplication.run(Application.class, args);
	}
}

OS Environment Variable

Define enviornment variable SERVER_SERVLET_CONTEXT_PATH with context path value.


set SERVER_SERVLET_CONTEXT_PATH=/technicalkeeda

Command Line Arguments

We can set the properties using command line arguments also.


java -jar SpringBoot-1.0.jar --server.servlet.context-path=/technicalkeeda

Tomcat started with specified context path

....
....
2019-04-13 12:34:47.643  INFO 1448 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2019-04-13 12:34:47.715  INFO 1448 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path '/technicalkeeda'
2019-04-13 12:34:47.723  INFO 1448 --- [           main] com.technicalkeeda.app.Application       : Started Application in 4.245 seconds (JVM running for 4.956)
Yashwant

Yashwant

Hi there! I am founder of technicalkeeda.com and programming enthusiast. My skills includes Java,J2EE, Spring Framework, Nodejs, PHP and lot more. If you have any idea that you would want me to develop? Lets connect: yashwantchavan[at][gmail.com]