Search code examples
javaspring-bootreactive-programmingspring-webflux

How can avoid breaking request flow in WebFilter when returning from inside another Mono<Object>>?


Here's some ridiculous code for example

@Component
public final class ReCaptchaCheckWebFilter implements WebFilter {
  @Override
  public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
    return Mono.just("foo").map(s -> chain.filter(exchange)).then();
  }
}

With this webfiler, the request never reaches my Controller. It does return a 200 http status code.

If I found myself needing to return from inside another Mono<Object>, how do I do that while forwarding the request along the filter chain and onto the controller?


Solution

  • chain.filter return a Mono, but in your example it is not a part of the stream (nothing subscribes to it). To fix that, use should use flatMap method

        return Mono.just("foo").flatMap(s -> chain.filter(exchange));