0
votes

I'm trying to solve this: How to rewrite URLs with Spring (Boot) via REST Controllers? by creating some kind of "filter" which would be applied to every incoming HTTP request.

The matter is covered by some answers like for this question: Spring Boot Adding Http Request Interceptors but interface HandlerInterceptor deals with javax' HttpServletRequest and HttpServletResponse which are not as practical as the new class introduced by Spring i.e. the ServerWebExchange (see the use of setLocation() in the code below) which appears in an interface whose name sounds promising, org.springframework.web.server.WebFilter:

So I ended with something like:

@Component
public class LegacyRestRedirectWebFilter implements WebFilter {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
        URI origin = exchange.getRequest().getURI();
        String path = origin.getPath();
        if (path.startsWith("/api/")) {
            ServerHttpResponse response = exchange.getResponse();
            response.setStatusCode(HttpStatus.MOVED_PERMANENTLY);
            URI location = UriComponentsBuilder.fromUri(origin).replacePath(path.replaceFirst("/api/", "/rest/")).build().toUri();
            response.getHeaders().setLocation(location);
        }
        return chain.filter(exchange);
    }

}

...in the same way people are doing something similar like:

Alas, my filter is never called!!!

The thing is: I am not in a "WebFlux" context (on the contrary to the questions above) because:

  1. I don't need to, and
  2. I tried and got the following problems:

Also I don't understand why would I need to be in a WebFlux context, because org.springframework.web.server.WebFilter neither deals with reactive nor Webflux, right? ..or does it? This is not very clear in the Javadoc.

1
your application is either a web application, or a webflux application. It cant be both, webfilter is part of webflux, so it will be loaded and used if your application is a webflux application. If you have both web and webflux on your classpath spring boot will per default start as a web application and you should be using a Filter baeldung.com/spring-boot-add-filter without seing your pom.xml/gradle its hard know what type of application you have.Toerktumlare
how can WebFilter be part of "WebFlux" as it's located in spring-web-5.1.9.RELEASE.jar?!maxxyme
btw, as you requested the pom.xml on another question of mine, I posted it here: stackoverflow.com/questions/63737318/…maxxyme
...also thanks for pointing me towards this (simple) Filter but after a quick search (albeit the Baeldung not specifying which Filter it is...) I found out this is the javax.servlet.Filter and as I said above, I'll try to avoid working with this as it's pure "javax" code and it deals with (Http)ServletRequest/(Http)ServletResponse, [see the casts in RequestResponseLoggingFilter].maxxyme
Well since i havn't seen your pom.xml (until now) many ppl add webflux to their spring-web application to use only the WebClient. Hence the default, you on the other hand, want to be using WebFilter since you want to build a webflux application. And your problem is that you are pulling in things that does not support webflux (springfox v2.9.2) so springfox is probably forcing your app to start as a web app.Toerktumlare

1 Answers

0
votes

I fact, I didn't make work WebFilter in a non-WebFlux context, but I could successfully implement such a filter, which both implements javax.servlet.Filter (non-reactive) AND org.springframework.web.server.WebFilter (reactive).

Here is my answer to the other related question: https://stackoverflow.com/a/63780659/666414