Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

Spring Boot, by default, serves content on the root context path (“/”).

While it’s usually a good idea to prefer convention over configuration, there are cases when we do want to have a custom path.

In this quick tutorial, we’ll cover the different ways of configuring it.

2. Setting the Property

Just like many other configuration options, the context path in Spring Boot can be changed by setting a property, server.servlet.context-path.

Note that this works for Spring Boot 2.x. For Boot 1.x, the property is server.context-path.

There are multiple ways of setting this property, so let’s look at them one by one.

2.1. Using application.properties / yml

The most straightforward way of changing the context path is to set the property in the application.properties/yml file:

server.servlet.context-path=/baeldung

Instead of putting the properties file in src/main/resources, we can also keep it in the current working directory (outside of the classpath).

2.2. Java System Property

We can also set the context path as a Java system property before the context is even initialized:

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

2.3. OS Environment Variable

Spring Boot can also rely on OS environment variables. On Unix based systems we can write:

$ export SERVER_SERVLET_CONTEXT_PATH=/baeldung

On Windows, the command to set an environment variable is:

> set SERVER_SERVLET_CONTEXT_PATH=/baeldung

The above environment variable is for Spring Boot 2.x.x.  If we have 1.x.x, the variable is SERVER_CONTEXT_PATH.

2.4. Command Line Arguments

We can set the properties dynamically via command line arguments as well:

$ java -jar app.jar --server.servlet.context-path=/baeldung

3. Using Java Config

Now let’s set the context path by populating the bean factory with a configuration bean.

With Spring Boot 2, we can use WebServerFactoryCustomizer:

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

With Spring Boot 1, we can create an instance of EmbeddedServletContainerCustomizer:

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

4. Priority Order of Configurations

With this many options, we may end up having more than one configuration for the same property.

Here’s the priority order in descending order, which Spring Boot uses to select the effective configuration:

  1. Java Config
  2. Command Line Arguments
  3. Java System Properties
  4. OS Environment Variables
  5. application.properties in Current Directory
  6. application.properties in the classpath (src/main/resources or the packaged jar file)

5. Conclusion

In this brief article, we covered the different ways of setting the context path, or any other configuration property, in a Spring Boot application.

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW