Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Context Path with Webflux

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.

like image 645
Marcus Lindwall Avatar asked Mar 09 '18 14:03

Marcus Lindwall


People also ask

What is context path in spring boot?

The context path is the name of the URL at which we access the application. The default context path is empty. The context path can be changed in many ways. We can set it in the properties file, with the SERVER_SERVLET_CONTEXT_PATH environment variable, with Java System property, or on the command line.

What is servlet context path?

The context path is the portion of the request URI that is used to select the context of the request. The context path always comes first in a request URI. The path starts with a "/" character but does not end with a "/" character. For servlets in the default (root) context, this method returns "".

What is the benefit of spring WebFlux?

Spring WebFlux makes it possible to build reactive applications on the HTTP layer. It is a reactive fully non-blocking, annotation-based web framework built on Project Reactor that supports reactive streams back pressure and runs on non-blocking servers such as Netty, Undertow and Servlet 3.1+ containers.


4 Answers

According to this

There is servlet in the name of the property which should be a hint that won't work with webflux.

With springboot v2.3, you can put this in your properties file

spring.webflux.base-path=/your-path

release-notes reference: https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.3-Release-Notes#configurable-base-path-for-webflux-applications

like image 171
Abudzar Al-Ghiffari Avatar answered Oct 03 '22 19:10

Abudzar Al-Ghiffari


You can use web filter to make WebFlux support contextPath

@Bean
public WebFilter contextPathWebFilter() {
    String contextPath = serverProperties.getServlet().getContextPath();
    return (exchange, chain) -> {
        ServerHttpRequest request = exchange.getRequest();
        if (request.getURI().getPath().startsWith(contextPath)) {
            return chain.filter(
                exchange.mutate()
                .request(request.mutate().contextPath(contextPath).build())
                .build());
        }
        return chain.filter(exchange);
    };
}
like image 41
刘家华 Avatar answered Oct 03 '22 18:10

刘家华


I was facing a similar issue with the spring.webflux.base-path (which didn't seem to be working as expected) in webflux-reactive-spring-web and I realized that I had autoconfiguration disabled.

A manually workaround is:

@Bean
public WebFluxProperties webFluxProperties(){
    return new WebFluxProperties();
}
like image 25
georgep Avatar answered Oct 03 '22 19:10

georgep


Here's my way of doing it with Tomcat Reactive:

@Configuration
public class TomcatReactiveWebServerConfig extends TomcatReactiveWebServerFactory {

    @Value("${server.servlet.context-path}")
    private String contextPath;

    /**
     * {@inheritDoc}
     */
    @Override
    protected void configureContext(final Context context) {

        super.configureContext(context);

        if (StringUtils.isNotBlank(this.contextPath)) {
            context.setPath(this.contextPath);
        }
    }
}
like image 41
Sancho Avatar answered Oct 03 '22 19:10

Sancho