W3docs

Add context path to Spring Boot application

To add a context path to a Spring Boot application, you can use the server.context-path property in the application's application.properties file.

To add a context path to a Spring Boot application, you can use the server.servlet.context-path property in the application's application.properties file. (Note: server.context-path is deprecated as of Spring Boot 2.2 and is kept only as an alias that may be removed in a future version.)

Here's an example of how you can set the context path in a Spring Boot application:


server.servlet.context-path=/myapp

This will set the context path of the application to "/myapp".

You can also configure it using YAML in application.yml:


server:
  servlet:
    context-path: /myapp

Alternatively, you can set the context path programmatically in your application's code by using the SpringApplication.setDefaultProperties method:


@SpringBootApplication
public class MyApplication {
  public static void main(String[] args) {
    SpringApplication app = new SpringApplication(MyApplication.class);
    app.setDefaultProperties(Collections.singletonMap("server.servlet.context-path", "/myapp"));
    app.run(args);
  }
}

This will set the context path to "/myapp" for the entire application.

Note that the context path is the base URL for the application, and is used to distinguish it from other applications that may be running on the same server. For example, if the context path is set to "/myapp", the application will be accessible at the URL "http://example.com/myapp".