5 ways : How to Change Context Path in Spring Boot

In this short tutorial, I will be sharing different ways to change the context path in Spring Boot. By default, the context path for the Spring Boot application is "/". In the below tutorial I will update the context path from  / to /javahungry or http://localhost:8080/javahungry

Read Also :  5 Ways to Change Default Port of Embedded Server in Spring Boot


The context path in Spring Boot can be updated by setting the value of the property server.servlet.context-path. This property works for Spring Boot 2.x.x and above.
For Spring Boot 1.x.x and below, the context path can be updated by setting the value of the property server.context-path.

1. Change Context Path Using a Properties file

Spring Boot does wonder by adding few lines of code in the application.properties. You just need to add server.servlet.context-path line in the application.properties

server.servlet.context-path=/javahungry 

2. Change Context Path Using Yaml file

If you are using yaml file for configuration instead of properties file then you need to make the following change in the application.yml file

server:
  servlet:   
     contextPath : /javahungry 

3. Change Context Path Using Command Line arguments

In case of a uber jar (contains java program and embeds its dependencies in a single jar file), using command-line arguments also you can change the context path.

java -jar application.jar --server.servlet.context-path=/javahungry

4. Change Context Path Using System Property

You can set server.servlet.context-path as System property using the main method as shown below :

package com.javahungry;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class JavaHungry {
public static void main(String args[])
{
System.setProperty("server.servlet.context-path","/javahungry");
// or try
// System.getProperties().put("server.servlet.context-path", "/javahungry");
SpringApplication.run(JavaHungry.class, args);
}
}

5. Change Context Path Programmatically

Set the context path by showing the bean factory with a configuration bean.

For Spring Boot 2.x.x and above use the following code :

package com.javahungry;

import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
public class AppCustomContainer
{
@Bean
public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory>
webServerFactoryCustomizer() {
return factory -> factory.setContextPath("/javahungry");
}
}

For Spring Boot 1.x.x and below use the following code :


package com.javahungry;

import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component; 
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer; 
 
@Component
public class AppCustomContainer
{
@Bean
public EmbeddedServletContainerCustomizer
embeddedServletContainerCustomizer() {
return container -> container.setContextPath("/javahungry");
}
}

6. Change Context Path Using OS Environment Variable

You can change the context path using OS-specific environment variables such as SERVER_SERVLET_CONTEXT_PATH.

For UNIX based systems we can write

$ export SERVER_SERVLET_CONTEXT_PATH=/javahungry 

 For Windows, set environment variable using below line

> set SERVER_SERVLET_CONTEXT_PATH=/javahungry 

The SERVER_SERVLET_CONTEXT_PATH environment variable is for Spring Boot 2.x.x and above.
For Spring Boot 1.x.x and below, use the SERVER_CONTEXT_PATH environment variable.

7. Configuration Priority Order

Programmatically changing the context path has the highest priority over other configurations. Command Line arguments take precedence over System properties, properties file or yaml file.

1. Programmatically
2. Command Line Arguments
3. System properties
4. OS Environment Variables
5. Properties or Yaml file

In this post, we have learned different ways to configure or change the context path in Spring Boot. Please mention in the comments in case you have any questions or doubts.

0 Comments