3

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.

Dreamer
  • 7,333
  • 24
  • 99
  • 179
  • It's using embeded tomcat, thus it has its own port, and no need the sub appname, you can config reversed proxy in nginx, and map an app name to the port. – Eric Oct 23 '18 at 22:11
  • 3
    Possible duplicate of [Add context path to Spring Boot application](https://stackoverflow.com/questions/20405474/add-context-path-to-spring-boot-application) – Passarinho Oct 23 '18 at 22:17

4 Answers4

7

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)

Nishant Bhardwaz
  • 924
  • 8
  • 21
0

Set the context path

  • Spring Boot 1.x: server.contextPath=/AppName
  • Spring Boot 2.x: server.servlet.contextPath=/AppName
Behe
  • 7,572
  • 3
  • 33
  • 46
0

You should use server.servlet.context-path for Spring Boot 2.x server.context-path for Spring 1.x in your application.properties file.

Passarinho
  • 109
  • 1
  • 2
  • 15
0

Add the following line in your application.properties (works with Spring Boot 1.x): server.contextPath=/AppName if your version is 2.x, the use the following: server.servlet.contextPath=/AppName

Tạ Anh Tú
  • 141
  • 1
  • 4