I've been trying to find a way to set the context path for a webflux application. I know I can configure it using
server.servlet.context-path
if I deploy a servlet, but I would like to achieve it with webflux, without having to explicitly add the path to every route or use MVC.
I was having the same issue since the loader balancer bases on the context path to route to different back end apps. One way to get around Spring Boot Webflux w/ the context path is using variables in the @XXXXMapping annotations. For instance @RequestMapping(value = "${server.servlet.context-path}/subpath")
According to this
With springboot v2.3, you can put this in your properties file
release-notes reference: https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.3-Release-Notes#configurable-base-path-for-webflux-applications
Here is an example of configuring the context path for WebFlux using Netty server based on a comment by @Dmytro Boichenko. You can also include customizers to configure the port and other properties.
For use cases where WebFlux application is behind load balancer/proxy you can use dedicated class -
ForwardedHeaderTransformer
that will extract path context fromX-Forwarded-Prefix
and will add it toServerHttpRequest
.Doing so you won't need to modify global
context-path
(which does not make sense in WebFlux)More about it here: https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html#webflux-web-handler-api
For Undertow I managed to add a context path by creating a customized UndertowReactiveWebServerFactory:
You can use web Filter, as mentioned in above answers, but you can do one more thing. Write a Base Controller and Extend every class to that Base Controller. For example:
Base Controller.java
NewController.java
So now you can hit /{base_url}/status