1. Overview

This article is about spring boot change context path. Application context path also called base path. If context path is specified then the application will be accessible like:

With context Path


Without Context Path


Make sure that your context must be start with / and does not end with / otherwise it will generate exception like : IllegalArgumentException : ContextPath must start with ‘/’ and not end with ‘/’

2. Different ways to change the context path

1. Change context path using application.properties or application.yml

server.context-path is properties using that application context path can be changed.




  context-path: /demo



2. Context path using the command line arguments

Another way is to change context path using command line argument while staring application at that time pass JVM arguments like :  -Dserver.contextPath. It will be useful if don’t want to hardcode context path in the application.

java -jar -Dserver.contextPath=/demo spring-boot-example-1.0-SNAPSHOT.jar



3. Context Path using EmbeddedServletContainerCustomizer

Context path also can be changed using EmbeddedServletContainerCustomizer interface which is as Bellow

package com.javadeveloperzone;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.stereotype.Component;
 * Created by JavaDeveloperZone on 31-03-2018.
public class ServletContainer implements EmbeddedServletContainerCustomizer {
    public void customize(ConfigurableEmbeddedServletContainer container) {
        container.setContextPath("/demo");      // specify context here



3. Conclusion

In this article, We learn about how to change context path in spring boot application sometimes developer forgot to add context path in URL and getting 404 page while configuring application which is developed by other developers so make sure that if context path is specified in the application then URL must start with the context path.

4. References



Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *