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

Viewed 8087

I am trying to create a spring-boot-2 REST api using spring-boot-starter-webflux and reactive Netty. I am trying to set the context-path as per the new properties to be defined in application.yml defined in Spring-Boot-2.

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

However it looks like Webflux, Netty doesn't use/recognise this property defined in application.yml.

If I use spring-boot-starter-web and Tomcat as the default server then it works fine and recognises context-path properly.

Didn't find anything mentioned about Netty's context-path in Spring Boot 2 documentation.

Spring Boot Version = 2.0.3.RELEASE

Please let me know if I missed something or this is the default behaviour of Webflux Netty ?

4 Answers

In spring boot 2.3.x you can set spring.webflux.base-path property

It worked for me with

spring.webflux.base-path=/myPath

but only when adding the hint in this comment: https://stackoverflow.com/a/67840678/8376373

which suggests to inject a WebFluxProperties Bean

@Bean
fun webFluxProperties(): WebFluxProperties {
    return WebFluxProperties()
}

You can use a WebFilter to work around this limitation:

    @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)
            }
        }
    }
Related