【问题标题】:change spring boot 2.0 context-path更改 spring boot 2.0 上下文路径
【发布时间】:2018-08-17 23:21:26
【问题描述】:

我想更改 Spring Boot 2 的上下文路径,例如我想在 http://localhost:8080/test/ 上服务

我的意思是它不适用于 spring-boot-starter-webflux:2.0.0.RELEASE

它只适用于 spring-boot-starter-web::2.0.0.RELEASE

我试过了

server.servlet.context-path=/test

但我什么也没发生,仍然在 url http://localhost:8080/ 上提供服务

【问题讨论】:

  • server.servlet.context-path 仅适用于嵌入式容器。你是如何部署你的应用程序的?
  • 它在 spring-boot-starter-web 上和我一起工作
  • 但不适用于 spring-boot-starter-webflux
  • 这是合乎逻辑的,因为 webflux 使用 netty 而不是 servlet 容器,所以我怀疑命名空间 server.servlet.* 中的属性是否适用于 webflux

标签: spring spring-boot spring-webflux


【解决方案1】:

如果您使用 servlet API,则现在调用该属性

server.servlet.context-path=/myapp

【讨论】:

  • 这适用于 Spring MVC。它不适用于 WebFlux。
  • 这就是我写“如果你使用 servlet API”的原因
  • 原来的问题是针对 Spring Boot 2 的。
  • @Gaurav 那么我们需要如何为 webflux 添加??
  • @prasannajoshi - 如果您使用带有 webflux 的 Netty 服务器,则没有上下文路径的概念。您可以在控制器端点上进行手动路由。但是,如果你想使用 Tomcat 服务器,你可以做上面的,它会工作。
【解决方案2】:

Spring Boot 团队的 Andy Wilkinson @andy-wilkinson 通过 Gitter 确认

WebFlux 中没有上下文路径的概念,因此没有等效的属性

即 WebFlux 不支持上下文路径配置

【讨论】:

【解决方案3】:

在 spring boot 2.3.x 中你可以设置 spring.webflux.base-path 属性

spring.webflux.base-path=/path

【讨论】:

    【解决方案4】:

    参考 Spring Boot 2.x。以下配置适用于application.yml

    server:
      port: 8080
      servlet:
        context-path: /test
    

    对于application.properties的配置是

    server.port=8080
    server.servlet.context-path= /test
    

    【讨论】:

      【解决方案5】:

      对于 WebFlux 应用程序位于负载均衡器/代理之后的用例,您可以使用专用类 - ForwardedHeaderTransformer,它将从 X-Forwarded-Prefix 提取路径上下文并将其添加到 ServerHttpRequest

      这样做你不需要修改全局上下文路径(这在 WebFlux 中没有意义)

      更多信息在这里:https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html#webflux-web-handler-api

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-10-07
        • 2018-10-01
        • 2019-08-22
        • 1970-01-01
        • 1970-01-01
        • 2014-10-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多