Add context path to Spring Boot application


I am trying to set a Spring Boot applications context root programmatically. The reason for the context root is we want the app to be accessed from localhost:port/{app_name} and have all the controller paths append to it.

Here is the application configuration file for the web-app.

public class ApplicationConfiguration {

  Logger logger = LoggerFactory.getLogger(ApplicationConfiguration.class);

  private String port;

  private String context;

  private Set<ErrorPage> pageHandlers;

  private void init(){
      pageHandlers = new HashSet<ErrorPage>();
      pageHandlers.add(new ErrorPage(HttpStatus.NOT_FOUND,"/notfound.html"));
      pageHandlers.add(new ErrorPage(HttpStatus.FORBIDDEN,"/forbidden.html"));

  public EmbeddedServletContainerFactory servletContainer(){
      TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();"Setting custom configuration for Mainstay:");"Setting port to {}",port);"Setting context to {}",context);
      return factory;

  public String getPort() {
      return port;

  public void setPort(String port) {
      this.port = port;

Here is the index controller for the main page.

public class IndexController {

  Logger logger = LoggerFactory.getLogger(IndexController.class);

  public String index(Model model){"Setting index page title to Mainstay - Web");
      model.addAttribute("title","Mainstay - Web");
      return "index";


The new root of the application should be at localhost:12378/mainstay, but it is still located at localhost:12378.

What am I missing that is causing Spring Boot to not append the context root before the request mapping?

2/28/2017 10:25:24 AM

Accepted Answer

Why are you trying to roll your own solution. Spring-boot already supports that.

If you don't already have one, add an file to src\main\resources. In that properties file, add 2 properties:


UPDATE (Spring Boot 2.0)

As of Spring Boot 2.0 (due to the support of both Spring MVC and Spring WebFlux) the contextPath has been changed to the following:


You can then remove your configuration for the custom servlet container. If you need to do some post processing on the container you can add a EmbeddedServletContainerCustomizer implementation to your configuration (for instance to add the error pages).

Basically the properties inside the serve as a default you can always override them by using another next to the artifact you deliver or by adding JVM parameters (-Dserver.port=6666).

See also The Reference Guide especially the properties section.

The class ServerProperties implements the EmbeddedServletContainerCustomizer. The default for contextPath is "". In your code sample you are setting the contextPath directly on the TomcatEmbeddedServletContainerFactory. Next the ServerProperties instance will process this instance and reset it from your path to "". (This line does a null check but as the default is "" it always fail and set the context to "" and thus overriding yours).

9/1/2017 5:39:57 AM

If you are using Spring Boot, then you don't have to configure the server properties via Vean initializing.

Instead, if one functionality is available for basic configuration, then it can be set in a "properties" file called application, which should reside under src\main\resources in your application structure. The "properties" file is available in two formats

  1. .yml

  2. .properties

The way you specify or set the configurations differs from one format to the other.

In your specific case, if you decide to use the extension .properties, then you would have a file called under src\main\resources with the following configuration settings

server.port = 8080
server.contextPath = /context-path

OTOH, if you decide to use the .yml extension (i.e. application.yml), you would need to set the configurations using the following format (i.e. YAML):

    port: 8080
    contextPath: /context-path

For more common properties of Spring Boot refer to the link below:


If you use Spring Boot 2.0.0 use:


please note that the "server.context-path" or "server.servlet.context-path" [starting from springboot 2.0.x] properties will only work if you are deploying to an embedded container e.g., embedded tomcat. These properties will have no effect if you are deploying your application as a war to an external tomcat for example.

see this answer here:


The correct properties are


to configure the path of the DispatcherServlet



to configure the path of the applications context below that.


We can change context root path using a simple entry in the properties file.

### Spring boot 1.x #########

### Spring boot 2.x #########