SpringTechnology

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 which is 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 "/".
This is why when we run a Spring Boot application, without any custom configurations, we can access its resources using

http://localhost:8080/

However, in some cases we may wish to change the context path of our Application. Hence, in the latest versions of Spring Boot, we can change the context path using a property server.servlet.context-path
On the other hand, in the older versions of Spring Boot, we can use server.context-path to modify the context path.

Using properties or yaml file

The simplest way of configuring Spring Boot Application Context path is to do so using application properties file.

server.servlet.context-path=/my-context

Or, application yaml file

server:
  servlet:
    context-path: /my-context

The properties defined in application properties or yaml files have the lowest priority. Hence other ways of setting the properties can override them.

Using Command Line Argument

As you might know, we can set any application level from the command line, when we execute the Spring Boot application JAR file.

 java -jar -Dserver.servlet.context-path="/my-context" spring-boot-demo.jar

The Command Line variables have higher priority over the Application level properties set using properties files or Java.

Using Java System Properties

In Java, we can set application level properties using System.setProperty method. Similarly, we can also set context-path as a System property.

System.setProperty("server.servlet.context-path", "/my-context");

The Command line variables have higher priority than System properties.

Using Environmental Variables

So far, the ways we have seen were specific to the application. However, we can also set the variable on the Operating System, where we will be deploying our application.

On a Unix based operating system we can use export command to set the environmental variable.

$ export SERVER_SERVLET_CONTEXT_PATH=/my-context

Similarly, for a Windows based operating system we can use set command which is similar to export.

 set SERVER_SERVLET_CONTEXT_PATH=/my-context

The environmental variables can override application properties.

So far, we have seen examples of setting context path in Spring Boot application using server.servlet.context-path variable. However, there is one more way of setting the context path. which we will see next.

Using Java Config

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

    @Bean
    public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> webServerFactoryCustomizer() {
        return factory -> factory.setContextPath("/my-context");
    }

Similarly, for older Spring Boot versions (1.x), we can do so by customising EmbeddedServletContainerCustomizer.

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

If we set the context path using Java Config, it will have the highest priority compared to all other ways of doing it.

Summary

In this quick tutorial we learned How to set context path in Spring Boot Application.
Spring boot uses the default context path as the root context ("/").

We have covered various ways of customising the context path in Spring Boot.