如何在Spring Boot应用程序中更改上下文路径?

在Spring Boot 2.x中我们可以通过在application属性文件中配置server.servlet.context-path来更改上下文路径,或者在命令行中使用java命令作为参数来更改上下文路径。

使用application属性配置:


server.servlet.context-path = /spring-boot-app
server.port = 80

使用Java命令行:
java -jar my-app.jar --server.servlet.context-path=/spring-boot-app --server.port=80

可以通过编程改变:


@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(MyApplication.class);
Map<String, Object> map = new HashMap<>();
map.put("server.servlet.context-path", "/spring-boot-app");
map.put("server.port", "80");
application.setDefaultProperties(map);
application.run(args);
}
}