This article will detail out different methods of context path configuration in Spring boot. Configuration might be to set or change the context path of Spring boot application.
What is context path
Context path is a name with which a web application is accessed. Context path comes right after the IP and port and is also called Context root. Example,

http://127.0.0.1:9090/myapp

In this URL, myapp is the context path or context root. Accessing the context path displays the home page of application.
By default, when you create a Spring boot web application, its context path is  “”, an empty string. So, it is accessed at

http://<ip>:<port>

But this can be changed.
In this article, we will explore 6 different ways and change the root URL of our application as http://localhost:8080/demoapp

Method 1: Using application.properties
Default behavior of Spring boot application can be customized by external properties. These properties are placed in application.properties file and are in the form of key-value pairs.

Property to change the context path of an application is server.servlet.context-path with its value as the path you want to set.
Thus, below line placed in application.properties will set the context path to demoapp.

server.servlet.context-path=/demoapp

Method 2: Using yaml
Similar to application.properties file, you can also use a yaml file for configuration. Property name remains the same and is declared as

server.
servlet.
context-path=/demoapp

Method 3: System property
Property server.servlet.context-path can also be set inside application code using setProperty() method of java.lang.System class.
You can set this property inside main class at application startup as shown below.

@SpringBootApplication
public class DemoAppApplication {

  public static void main(String[] args) {
    System.setProperty("server.servlet.context-path", "/demoapp");
    SpringApplication.run(DemoAppApplication.class, args);
  }

}

Method 4: Command line configuration
In java, system property can be set from command line while running the application as a jar using -D flag. Thus, setting server.servlet.context-path property from command line also configures the context path as.

java -jar -Dserver.servlet.context-path=/demoapp demoapp.jar

Same can also be done with — flag as

java -jar demoapp.jar – -server.servlet.context-path=/demoapp

This method does not require any changes in code base as they are external to the application.

Method 5: Programmatic configuration
Create an object or bean of org.springframework.boot.web.server.WebServerFactoryCustomizer. This is an interface of generic type.
For customizing application configuration such as address or changing port of a Spring boot application, a bean of WebServerFactoryCustomizer needs to be created.
Generic type of this interface should be ConfigurableServletWebServerFactory as shown below.

public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> configureServer(){
  return new WebServerFactoryCustomizer<ConfigurableServletWebServerFactory>() {

    @Override
    public void customize(ConfigurableServletWebServerFactory factory) {
      factory.setContextPath("/demoapp");
      
    }
  };

WebServerFactory has a single method customize() which takes a generic argument of the same type as the type of the interface.
Since the generic type of interface is ConfigurableServletWebServerFactory, the type of argument to customize() is also the same.
ConfigurableServletWebServerFactory has a method setContextPath() which takes a string argument and represents the context path that you want to configure for the application.

Remember that this method should be registered as a Spring bean. So you need to write this method in a Configuration class and annotate it with @Bean annotation as shown below.

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

@Configuration
public class AppCustomizer {
   @Bean	
   public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> configureServer(){
     return new WebServerFactoryCustomizer<ConfigurableServletWebServerFactory>() {

       @Override
       public void customize(ConfigurableServletWebServerFactory factory) {
          factory.setContextPath("/demoapp");
       }
     };
   }
}

Finally, since WebServerFactoryCustomizer interface has a single method, it is a Functional interface and hence it can be represented as a Lambda expression and can be implemented as below.

public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> configureServer(){
  return cfgFactory -> cfgFactory.setContextPath("/demoapp");
}

In the previous example, WebServerFactoryCustomizer was implemented as an anonymous inner class.

With Spring boot 1.x, this code will not work, you need to use EmbeddedServletContainerCustomizer as below.

import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppCustomizer {

  @Bean
  public EmbeddedServletContainerCustomizer configureServer() {
     return new EmbeddedServletContainerCustomizer() {
		
	@Override
	public void customize(ConfigurableEmbeddedServletContainer container) {
	   container.setContextPath("/demoapp");
				
	}
     };
  }
}

Here, EmbeddedServletContainerCustomizer is an interface.
Again, with Lambda expression, this can be modified as below.

@Bean
public EmbeddedServletContainerCustomizer configureServer() {
   container.setContextPath("/demoapp");
}
Method 6: Using OS environment variables
Spring boot can also read application configuration from environment variables of the underlying operating system.
Thus, if your application is running on UNIX/Linux based systems, its context path can be set using below environment variable

$ export SERVER_SERVLET_CONTEXT_PATH=/demoapp

If the application runs on Windows, then below syntax should be used.

C:/ set SERVER_SERVLET_CONTEXT_PATH=/demoapp

These variables are for Spring boot 2.x. If your Spring boot version is 1.x, then use
SERVER_CONTEXT_PATH

Order of Preference
If you have used more than one methods to set context path, then following will be the order of preference followed by Spring boot.

1. Programmatic Configuration. This will override all other settings.
2. Command Line configuration. Will work if context path is not set using a configuration bean.
3. System properties set inside application code.
4. OS environment variables.
5. application.properties file will be looked if context path is not configured anywhere else.

If you liked the post, do not forget to hit the clap below.

Leave a Reply