简体   繁体   中英

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?

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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM