java spring spring-boot spring-mvc

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?