Monday , September 25 2017
Home / Spring Boot / How To Change Spring Boot Context Path

How To Change Spring Boot Context Path

In this tutorials, I am going to show you how to change the Spring Boot Context path.

Spring Boot Context Path  :

Like changing the server port in spring boot, to change the context path in Spring Boot we have 3 ways.

  • By updating the application.properties file,
  • By configuring the Embedded Servlet Container and
  • By passing the arguments while running the application

Lets see the above 3 scenarios one by one,

application.properties

Spring Boot gives us a property called server.context-path which is used to change the tomcat port from properties file.

To use this, you can update the application.properties like below :

server.port=8085
server.context-path=/Basic_Spring_Boot

Configuring the Embedded Servlet Container :

We can also change the Spring Boot Server Context by obtaining the ConfigurableEmbeddedServletContainer object. It allows us to change the runtime server properties like change the contextPath, serverPort, sessionTimeOut and etc.,


package com.onlinetutorialspoint.spring.boot;

import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.stereotype.Component;

@Component
public class CustomContainer implements EmbeddedServletContainerCustomizer {

    @Override
    public void customize(ConfigurableEmbeddedServletContainer container) {

        container.setContextPath("/Basic_Spring_Boot");

    }

}
}

The context path should be starts with “/” and should not ends with “/”, other wise we will get BeanCreationException.

Arguments while running the application :


-Dserver.context-path=/Basic_Spring_Boot

Output :

Spring Boot Context Path

Based on your requirement, you can use any one among 3 possibilities.

Happy Learning 🙂

About chandrashekhar

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Recommended

Spring Boot FileUpload Ajax Example

In this tutorials, we are going to see how to work with Spring Boot Fileupload …

Leave a Reply

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