So how to add the AppName in the context?
Spring Boot, by default, serves content on the root context path (“/”), But we can change it in different ways.
1) Using application.properties / yml
For Boot 1.x, the property is server.context-path=/AppName
For Boot 2.x, the property is server.servlet.context-path=/AppName
2) Using Java system property
public static void main(String[] args) {
System.setProperty("server.servlet.context-path", "/AppName");
SpringApplication.run(Application.class, args);
}
3) Using OS Environment Variable
On Linux:- $ export SERVER_SERVLET_CONTEXT_PATH=/AppName
On Windows:- set SERVER_SERVLET_CONTEXT_PATH=/AppName
4) Using Command Line Arguments
$ java -jar app.jar --server.servlet.context-path=/AppName
5) Using Java Config
With Spring Boot 2, we can use WebServerFactoryCustomizer
:
@Bean
public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory>
webServerFactoryCustomizer() {
return factory -> factory.setContextPath("/AppName");
}
With Spring Boot 1, we can create an instance of EmbeddedServletContainerCustomizer
:
@Bean
public EmbeddedServletContainerCustomizer
embeddedServletContainerCustomizer() {
return container -> container.setContextPath("/AppName");
}
Note:- Priority order in descending order, which Spring Boot uses to select the effective configuration:
Java Config
Command Line Arguments
Java System Properties
OS Environment Variables
application.properties in Current Directory
application.properties in the classpath (src/main/resources or the packaged jar file)