Spring Boot 2 Netty servlet.context-path不起作用

spring boot 2 + netty + servlet.context-path + not working

我正在尝试使用spring-boot-starter-webflux和反应性Netty创建spring-boot-2 REST api。我试图根据要在Spring-Boot-2中定义的application.yml中定义的新属性设置上下文路径。

1
server.servlet.context-path: /api  # Define the server context path

但是,看起来像Webflux的Netty不会使用/识别在application.yml中定义的此属性。

如果我使用spring-boot-starter-web和Tomcat作为默认服务器,则它可以正常工作并正确识别上下文路径。

在Spring Boot 2文档中找不到有关Netty的context-path的任何内容。

1
Spring Boot Version = 2.0.3.RELEASE

请让我知道我是否错过了某些东西,或者这是Webflux Netty的默认行为吗?


Configuring the context path is servlet-specific. when using WebFlux,
the configuration property was renamed to server.servlet.context-path and only for servlet based deployment.

您可以在下面的线程中阅读如何在webflux中处理上下文路径,请参阅注释

https://github.com/spring-projects/spring-boot/issues/10129#issuecomment-351953449

Webflux上下文路径发布线程


在Spring Boot 2.3.x中,您可以设置spring.webflux.base-path属性


您可以使用WebFilter来解决此限制:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    @Autowired
    lateinit var serverProperties: ServerProperties

    @Bean
    fun contextPathWebFilter(): WebFilter {
        val contextPath = serverProperties.servlet.contextPath
        return WebFilter { exchange, chain ->
            val request = exchange.request
            if (request.uri.path.startsWith(contextPath)) {
                chain.filter(
                        exchange.mutate()
                                .request(request.mutate().contextPath(contextPath).build())
                                .build())
            } else {
                chain.filter(exchange)
            }
        }
    }