Ebooks

Spring Boot context path

Spring Boot context path turorial shows how to setting context path in a Spring application.

Spring is a popular Java application framework and Spring Boot is an evolution of Spring that helps create stand-alone, production-grade Spring based applications easily.

Context path

The context path is the name of the URL at which we access the application. The default context path is empty.

The context path can be changed in many ways. We can set it in the properties file, with the SERVER_SERVLET_CONTEXT_PATH environment variable, with Java System property, or on the command line.

$ export SERVER_SERVLET_CONTEXT_PATH=/myapp

On Unix systems, we can change the SERVER_SERVLET_CONTEXT_PATH with the export command.

> set SERVER_SERVLET_CONTEXT_PATH=/myapp

On Windows, it is done with the set command.

server.servlet.context-path=/myapp

The property can be change in the application.properties file. (Also in the application.yml file.)

@Configuration
public class AppConfig {

    @Bean
    public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory>
        webServerFactoryCustomizer() {

        return factory -> factory.setContextPath("/myapp");
    }
}

The context path can be set with the WebServerFactoryCustomizer bean.

@SpringBootApplication
public class Application  {

    public static void main(String[] args) {

        System.setProperty("server.servlet.context-path", "/myapp");
        SpringApplication.run(Application.class, args);
    }
}

Here we set the context with the Java System property.

new SpringApplicationBuilder(Application.class)
    .bannerMode(Banner.Mode.OFF)
    .properties("server.servlet.context-path=/myapp")
    .logStartupInfo(false)
    .build()
    .run(args);

Here we set the context path as the default property using the SpringApplicationBuilder.

$ java -jar -Dserver.servlet.context-path=/test target/SpringBootContextPath-1.0-SNAPSHOT.jar

Here we set the context path on the command line.

Spring gives these options different priorities. The following list shows the prorities in descending order.

The higher the option, the higher the priority.

Spring Boot context path example

In the following application, we set a custom context path. Several options are commented. Play with these options to the idea of their priorities.

... Tomcat started on port(s): 8080 (http) with context path '/myapp'

When the application starts, Spring Boot shows an information message about the chosen context path on the terminal.

pom.xml
src
├───main
│   ├───java
│   │   └───com
│   │       └───zetcode
│   │           │   Application.java
│   │           ├───config
│   │           │       AppConfig.java
│   │           └───controller
│   │                   MyController.java
│   └───resources
│           application.yml
└───test
    └───java

This is the project structure.

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
         http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.zetcode</groupId>
    <artifactId>SpringBootContextPath</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
    </parent>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

This is the Maven build file.

resources/application.yml
server:
  servlet:
    context-path: /myapp3

In the application.yml file we write various configuration settings of a Spring Boot application. We can set the context path here. This configuration can be overwritten using other options with higher priority, such as @Bean configuration.

com/zetcode/config/AppConf.java
package com.zetcode.config;

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 AppConfig {

    @Bean
    public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory>
        webServerFactoryCustomizer() {

        return factory -> factory.setContextPath("/myapp1");
    }
}

Here we set the context path with the WebServerFactoryCustomizer bean.

com/zetcode/controller/MyController.java
package com.zetcode.controller;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class MyController {

    @GetMapping(value = "/", produces = MediaType.TEXT_PLAIN_VALUE)
    public @ResponseBody String home() {

        return "home page";
    }
}

MyController handles a request from the client. For the home page, it returns a simple text value.

com/zetcode/Application.java
package com.zetcode;

import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {

//        System.setProperty("server.servlet.context-path", "/myapp2");
//        SpringApplication.run(Application.class, args);

        new SpringApplicationBuilder(Application.class)
                .bannerMode(Banner.Mode.OFF)
                .properties("server.servlet.context-path=/myapp4")
                .logStartupInfo(false)
                .build()
                .run(args);
    }
}

Application is the entry point which sets up Spring Boot application. Here we can also set the context path; either with a Java System property, or with the default properties of the builder.

In this tutorial, we have shown how to set a context path in a Spring Boot application.

List all Spring Boot tutorials.