7
votes

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 ?

3

3 Answers

5
votes

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.

You can read below thread to how you can deal with context path in webflux, please see comment

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

Webflux Context path issue thread

3
votes

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

0
votes

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