Spring boot application start without proper application name

Dreamer :

When running

./mvnw spring-boot:run

current spring boot application can open in the browser with current URL

http://localhost:8080/

but not

http://localhost:8080/AppName

So even in Swagger the APIs has to retrieve like this

http://localhost:8080/api/swagger.json

instead of this

http://localhost:8080/AppName/api/swagger.json

So how to add the AppName in the context? Easy in the old days where web.xml is xml based, in java based config I have add

spring.application.name=AppName

but still don't resolve the issue.

Nishant Bhardwaz :

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)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=113579&siteId=1