spring - 我们可以在同一个端口上运行多个 spring boot 应用程序吗?如果没有,为什么我们在 application.properties 中有上下文路径选项?

标签 spring spring-boot

server.port=8080
server.contextPath=/myapp

考虑 Spring Boot 应用程序的 application.properties 中的上述配置。如果我们不能在同一个端口 8080 中真正运行另一个 spring boot 应用程序,那么需要 contextPath 什么。谁能提供一些这个 contextPath 有用的实时场景?

[编辑]
根据我的经验,当我们将多个 Web 应用程序(Web 应用程序上下文)部署到在 8080 端口上运行的单个 tomcat 实例时,上下文路径用于区分应用程序。这就是让我思考在单个端口中运行多个 spring 应用程序的可能性的原因。否则,我看不到更改 contextPath 选项的值(value)。还考虑到我们将使用 nginx 或 apache 作为 Web 服务器前端的事实,并且这个 Spring 应用程序将仅在防火墙内运行。

最佳答案

  • 不,您一次只能让一个应用程序监听特定端口。
  • 更改上下文路径有助于为所有 Controller 端点全局添加前缀。
    例如,如果您的应用程序正在提供 rest API,并且您希望所有 url 以 /api 开头。那么一种选择是将此前缀添加到每个请求映射(例如 @RequestMapping("/api/user) ),或者将上下文路径更改为 /api那么您现在可以将您的请求映射为 @RequestMapping("/user)
  • 关于spring - 我们可以在同一个端口上运行多个 spring boot 应用程序吗?如果没有,为什么我们在 application.properties 中有上下文路径选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55902019/

    相关文章:

    java - Spring Boot请求验证不起作用

    java - Spring 方法存在于代码中,但不存在于 JAR 中

    java - CrudRepository OperationNotSupported

    spring-boot - Spring Boot + Spring 集成 Java DSL + AOP : Fails to proxy the Gateway interface

    java - Cloudant 429 模型过多

    java - Spring Autowiring /Java

    java - SpringMVC 如何在调用构造函数时从 session 范围获取 bean

    spring - 如何使用 Spring Data JPA 连接两个表

    spring - spring-mvc 中的 Autowiring 对象是线程安全的吗?

    spring - 为什么未验证 ConfigurationProperties?