In Spring Boot, you can change the context path of your application by setting the server.servlet.context-path property in the application.properties or application.yml file.

Using Application Properties file:

For example, if you want to set the context path to /myapp, you would add the following line to the application.properties file:

server.servlet.context-path=/myapp

Using Application Properties file: application.yml

server:
    servlet:
        context-path: /myapp

Change context path programmatically:

@Configuration
public class ServletConfig {

       @Value("${server.servlet.context-path}")
       String contextPath;

      @Bean
     public ServletWebServerFactory servletContainer() {
         TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
         tomcat.addAdditionalTomcatConnectors(createStandardConnector());
         tomcat.setContextPath(contextPath);
         return tomcat;
      }    

}

Change context path using command line arguments

for example --server.servlet.context-path=/myapp

java -jar my-app.jar --server.servlet.context-path=/myapp --server.port=8585

It is important to notice that the context path should start with a forward slash (“/”) and should not end with a forward slash, otherwise it will not work as expected.