8

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 Answers4

6

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

Anton Simonov
  • 61
  • 1
  • 2
  • have no words to thank you. You saved me so much time. It worked for me simply by adding *spring.webflux.base-path=/api/v1* to **application.properties**. My Spring Boot v2.6.4 – George Smith Nov 24 '22 at 20:55
5

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

kj007
  • 6,073
  • 4
  • 29
  • 47
2

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()
}
evainga
  • 41
  • 4
0

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)
            }
        }
    }
codependent
  • 23,193
  • 31
  • 166
  • 308