How to Change Context Path in Spring Boot

This quick tutorial covers various ways to Change the Context Path in a Spring Boot Application with helpful examples.

Context Path in Spring Boot

A context path is a prefix to the URL path used to identify and differentiate between different context(s).

In a Spring Boot application, the server sets the context path to the root, which means the ‘/‘. This is why we can access a Spring Boot application’s resources in the root path.

http://localhost:8080/Code language: plaintext (plaintext)

However, in some cases, we may wish to change the context path of an application. Thankfully, Spring Boot allows us to modify the default context path using the ‘server.servlet.context-path‘ property.

Please note that in the older versions of Spring Boot, the context path can be set using the ‘server.context-path’ property.

Update Context Path using YAML

We can use Spring Boot’s application properties or YAML file to set a custom context path to a Spring Boot application.

Custom Context Path using the Application Properties file

server.servlet.context-path=/my-contextCode language: Properties (properties)

Alternatively, the same can be done using a YAML file.

Custom Context Path using the Application YAML file

server:
  servlet:
    context-path: /my-contextCode language: YAML (yaml)

Remember that the configurations within Application Properties or YAML files have the lowest priority and can be overridden by other configuring methods.

Update Context Path from Command Line Argument

Spring Boot allows us to override the default configuration or the configurations from a Properties or YAML file using command line arguments. We can use command line arguments to set or modify our Spring Boot application’s context path.

Custom context path using command line arguments.

java -jar \
  -Dserver.servlet.context-path="/my-context" \
  spring-boot-demo.jarCode language: Bash (bash)

The Context Path set using the command line arguments has higher priority over the one defined in an Application Properties or YAML.

Update Context Path using Java System Properties

Java allows us to set application-level configurations in System Properties. We can use them to set a custom context path for our Spring Boot application.
Custom context path using Java System Properties.

System.setProperty(
    "server.servlet.context-path", "/my-context");Code language: Java (java)

The Command line variables have higher priority than System properties.

Update Context Path using Environment Variables

The ways we have studied so far are specific to an application. However, we can also set the context path using an environment variable on the machine or inside the application container.

Spring Boot allows us to set or modify an application’s context path by setting the ‘SERVER_SERVLET_CONTEXT_PATH‘ property as an environment variable.

Example of updating context path as an environment variable on a Unix-based machine.

$ export SERVER_SERVLET_CONTEXT_PATH=/my-contextCode language: Bash (bash)

Example of updating context path as an environment variable on a Windows-based machine

set SERVER_SERVLET_CONTEXT_PATH=/my-contextCode language: Bash (bash)

The environmental variables can override application properties.

Update Context Path programmatically

In Spring Boot, we can customise the server configurations like context path, port, server address, and error page details using the WebServerFactoryCustomizer. To do that, we need to provide a @Bean factory method to return a customised instance of this class.

Update context path programmatically.

@Bean
public WebServerFactoryCustomizer 
    webServerFactoryCustomizer() {
  return factory -> f
      actory.setContextPath("/my-context");
}Code language: Java (java)

Similarly, for older Spring Boot versions (1.x), we can do so by customising the EmbeddedServletContainerCustomizer.
Update context path programmatically on older Spring Boot applications.

@Bean
public EmbeddedServletContainerCustomizer 
    embeddedServletContainerCustomizer() {
  return container -> 
      container.setContextPath("/my-context");
}Code language: Java (java)

This type of configuration has the highest priority over all other ways.

Summary

This tutorial covered ways to update or change Spring Boot Application’s default context path. By default, the root – ‘/‘ is the default context path of all Spring Boot applications.

However, we can change the context path using ‘server.servlet.context-path‘. We can set this property using application properties, YAML, command line argument, Java System property, or an environment variable. Alternatively, we can programmatically update a Spring Boot application’s context path.