230

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.

@Configuration
public class ApplicationConfiguration {

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

  @Value("${mainstay.web.port:12378}")
  private String port;

  @Value("${mainstay.web.context:/mainstay}")
  private String context;

  private Set<ErrorPage> pageHandlers;

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

  @Bean
  public EmbeddedServletContainerFactory servletContainer(){
      TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
      logger.info("Setting custom configuration for Mainstay:");
      logger.info("Setting port to {}",port);
      logger.info("Setting context to {}",context);
      factory.setPort(Integer.valueOf(port));
      factory.setContextPath(context);
      factory.setErrorPages(pageHandlers);
      return factory;
  }

  public String getPort() {
      return port;
  }

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

Here is the index controller for the main page.

@Controller
public class IndexController {

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

  @RequestMapping("/")
  public String index(Model model){
      logger.info("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?

1

22 Answers 22

473

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

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

server.contextPath=/mainstay
server.port=12378

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:

server.servlet.context-path=/mainstay

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 application.properties serve as a default you can always override them by using another application.properties 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).

6
  • Altough your answer is correct (I mean, you can customize the servlet container properties by the the application properties file), why is there a setContextPath(path) method, I mean, if the path is not enforced, what is it good for? BTW, same goes for the setContextPath(...) on the EmbeddedServletContainerCustomizer
    – Modi
    Feb 10, 2014 at 13:32
  • 2
    I would expect the one from EmbeddedServletContainerCustomizer to also work. But I would go what is provided instead of trying to bolt on your own. Why your solution doesn't work has to do with the default behavior programmed (accidentally?) in the ServerProperties, the default configured contextPath is "" (and it checks for null and not "". The latter overrides your explicitly set contextPath.
    – M. Deinum
    Feb 10, 2014 at 14:08
  • The properties have changed, see my answer below. Apr 24, 2017 at 9:04
  • 8
    I guess the property in Spring Boot 2.0 is "server.servlet.context-path"
    – IamVickyAV
    Jan 21, 2019 at 7:30
  • 1
    Maybe helpful for someone: for context-path to work, spring boot project must be a web-project. <artifactId>spring-boot-starter-web</artifactId> must be added as dependency
    – S Kumar
    Dec 15, 2021 at 11:54
42

If you are using Spring Boot, then you don't have to configure the server properties via Bean 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 application.properties 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):

server:
    port: 8080
    contextPath: /context-path

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

https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

29

If you use Spring Boot 2.0.0 use:

server.servlet.context-path
6
  • 3
    this doesn't work for war files deployed in external tomcat
    – pise
    Dec 22, 2018 at 18:18
  • 1
    For me this didn't worked (Spring Boot 2.1.2), but did worked this: server.servlet.contextPath=/api
    – lospejos
    Feb 3, 2019 at 20:31
  • 2
    @pise, did you know how to fix it for war file in external tomcat?
    – mohax
    Sep 14, 2019 at 19:33
  • @mohax if you deploy WAR file to external tomcat, you've to rename the WAR file to ROOT.war to make thing work as expected.
    – Duc Le
    Aug 20, 2020 at 10:15
  • @DucLe in my case I just rename war file to needed path. I.e. servicename#api, where # is replaced by / by tomcat.
    – mohax
    Aug 20, 2020 at 10:54
15

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: https://stackoverflow.com/a/43856300/4449859

4
  • Has anyone figured out how to configure this when deploying to an external tomcat as war file using springboot v2.x and tomcat v8.5? Nov 23, 2018 at 20:00
  • @abdel even I looking for the answer, what if we are deploying the war file in external tomcat how to set context path.
    – pise
    Dec 22, 2018 at 18:17
  • I was trying it out. It's exactly like what it was stated in the link above. Change the build -> finalName attribute value to your context path. The resulting war file will then be using the context path as the filename, which will then be used by tomcat to serve as the context path. Feb 22, 2019 at 3:10
  • the only way i can think of for deploying as war in external tomcat is to ensure the war name matches the context you are after. so for example, if you want the context to be '/xyzwebapp' then your war must be named xyzwebapp.war. to achieve this, you can add the following to the <build> element in your pom.xml : <finalName>xyzwebapp</finalName>.
    – abdel
    Apr 5, 2019 at 10:34
11

The correct properties are

server.servlet.path

to configure the path of the DispatcherServlet

and

server.servlet.context-path

to configure the path of the applications context below that.

0
5

You can do it by adding the port and contextpath easily to add the configuration in [src\main\resources] .properties file and also .yml file

application.porperties file configuration

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

application.yml file configuration

server:
port: 8084
contextPath: /context-path

We can also change it programmatically in spring boot.

@Component
public class ServerPortCustomizer implements     WebServerFactoryCustomizer<EmbeddedServletContainerCustomizer > {

@Override
public void customize(EmbeddedServletContainerCustomizer factory) {
    factory.setContextPath("/context-path");
    factory.setPort(8084);
}

}

We can also add an other way

@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {SpringApplication application =     new pringApplication(MyApplication.class);
    Map<String, Object> map = new HashMap<>();
    map.put("server.servlet.context-path", "/context-path");
    map.put("server.port", "808");
    application.setDefaultProperties(map);
    application.run(args);
    }       
}

using java command spring boot 1.X

java -jar my-app.jar --server.contextPath=/spring-boot-app     --server.port=8585 

using java command spring boot 2.X

java -jar my-app.jar --server.servlet.context-path=/spring-boot-app --server.port=8585 
1
  • we can also add the server port programatically Jul 23, 2019 at 15:58
5

If you're using spring-boot-starter-webflux use:

spring:
  webflux:
    base-path: /api

I swear to God... I forget this every single time.

1
  • THIS! is what i needed for a spring boot 2.7.5 with spring-boot-starter-webflux but without spring-boot-starter-web
    – n99
    Nov 3, 2022 at 8:03
5

If you are using application.yml and spring version above 2.0 then configure in the below manner.

server:
  port: 8081
  servlet:
     context-path: /demo-api

Now all the API call will be like http://localhost:8081/demo-api/

1
  • This is worked for me. thanks mate
    – Pranav MS
    Nov 20 at 19:43
2

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

application.properties

### Spring boot 1.x #########
server.contextPath=/ClientApp

### Spring boot 2.x #########
server.servlet.context-path=/ClientApp
2

For below Spring boot 2 version you need to use below code

server:
   context-path: abc    

And For Spring boot 2+ version use below code

server:
  servlet:
    context-path: abc
1

We can set it in the application.properties as API_CONTEXT_ROOT=/therootpath

And we access it in the Java class as mentioned below

@Value("${API_CONTEXT_ROOT}")
private String contextRoot;
0
1

server.contextPath=/mainstay

works for me if i had one war file in JBOSS. Among multiple war files where each contain jboss-web.xml it didn't work. I had to put jboss-web.xml inside WEB-INF directory with content

<?xml version="1.0" encoding="UTF-8"?>
<jboss-web xmlns="http://www.jboss.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.jboss.com/xml/ns/javaee http://www.jboss.org/j2ee/schema/jboss-web_5_1.xsd">
    <context-root>mainstay</context-root>
</jboss-web>
1
  • It's not working with Wildfly23. Any idea?
    – Ravi MCA
    Oct 22, 2021 at 12:58
1

In Spring Boot 1.5:

Add the following property in application.properties:

server.context-path=/demo

Note: /demo is your context path URL.

1

We can set it using WebServerFactoryCustomizer. This can be added directly in the spring boot main method class which starts up the Spring ApplicationContext.

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

If you use Spring Boot 2.x and want to pass the context path property in the command line, you should put double // like this:

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

That worked for me running in windows.

1

It must be: server.servlet.context-path = / demo note that it does not have quotes only the value preceded by '/' this value goes in your application.properties file

1

You can use in Spring Boot: 2.1.6 like below:

server.servlet.context-path=/api-path
1

You can add this property to spring property file

spring.data.rest.basePath=/your_path
1

The accepted answer works. With SpringBoot 3.0, this works:

server.servlet.context-path=/users
0
<!-- Server port-->

server.port=8080

<!--Context Path of the Application-->

server.servlet.context-path=/ems
2
  • Server port will be 8080. if you want any other port, you can replace 8080. Fro application context path i have set ems. You can set other path as per your requirement Apr 15, 2020 at 18:13
  • 1
    That's useful information, why don't you add it into your answer instead of a comment?
    – k-den
    Apr 15, 2020 at 19:37
0

I can see many outdated answers / context-path formats. Please use latest ones.

application.yml

server:
  servlet:
    context-path: /hello-context-path

application.properties

server.servlet.context-path=/hello-context-path
-1

context path can be directly integrated to the code but it is not advisable since it cannot be reused so write in the application.properties file server.contextPath=/name of the folder where you placed the code contextPath = name of the folder where you placed the code/ Note:watch the slash carefully.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.