简体   繁体   English

如何有条件地从 WebFilter 中的 Mono 返回?

[英]How do I conditionally return from a Mono within a WebFilter?

Not sure I'm asking this right so here's an example of what I want to do (in imperative style with blocking)不确定我问的对不对,所以这是我想做的一个例子(以阻塞的命令式风格)

public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
  Mono<MyThing> myThingMono = getMyThingMono();
  // I Know this is not right but it's effectively what I want to do
  if( myThingMono is empty ) {
    return chain.filter(exchange);
  } else {
    MyThing thing = myThingMono.block(); // I know I can't do this
    switch(thing.status) {
      case A:
        exchange.getResponse().setStatus(HttpStatus.BAD_REQUEST);
        return exchange.getResponse().setComplete();
      default: 
        return chain.filter(exchange);
    }
  }
}

This is the closest I've gotten in an reactive way这是我以被动方式获得的最接近的

public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
  Mono<MyThing> myThingMono = getMyThingMono();
  myThingMono.map(thing -> {
    switch(thing.status) {
      case A: return HttpStatus.BAD_REQUEST;
      default: return HttpStatus.OK;
    }        
  })
  .defaultIfEmpty(HttpStatus.OK) // in case myThingMono is empty
  .map(status -> {
    switch(status) {
      case HttpStatus.OK:
        return chain.filter(exchange);
      default:
        exchange.getResponse().setStatusCode(status);
        return exchange.getResponse().setComplete();
    }        
  })
  .then();

The myThingMono wants to return aa Mono<Object> but my filter is expecting a Mono<Void> so I just jammed a .then() into it. myThingMono 想要返回 aa Mono<Object>但我的过滤器期待Mono<Void>所以我只是将.then()塞入其中。 I'm sure that's not right.我确定这是不对的。

My code compiles and is reaching the final switch statement and calling the right return statement, but the request is not reaching my controller .我的代码编译并到达最终的 switch 语句并调用正确的 return 语句,但请求没有到达我的 controller The webfilter is not forwarding the request correctly.网络过滤器未正确转发请求。 I'm just getting a 200 status back without the handler's result.我只是在没有处理程序结果的情况下返回 200 状态。

What is the right reactive way to do this?什么是正确的反应方式来做到这一点?

I meant something like this.我的意思是这样的。 I used .switchIfEmpty(Mono.empty()) to return something if your code does not enter on the first transformation.如果您的代码在第一次转换中没有输入,我使用.switchIfEmpty(Mono.empty())返回一些东西。 You can also create a default Mono.just(new MyThing()) .您还可以创建一个默认Mono.just(new MyThing())

Then I used the flatMap to place all the logic switch... inside it.然后我使用flatMap将所有逻辑switch...放在里面。

enum MyThingEnum { A }

class MyThing { public MyThingEnum status; }

public class Test {
    public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
        Mono<MyThing> myThingMono = getMyThingMono();
        return myThingMono.flatMap(thing -> {
            switch (thing.status) {
                case A:
                    exchange.getResponse().setStatusCode(HttpStatus.BAD_REQUEST);
                    return exchange.getResponse().setComplete();
                default:
                    exchange.getResponse().setStatusCode(HttpStatus.OK);
                    return chain.filter(exchange);
            }
        })
        .switchIfEmpty(Mono.empty());
    }

    private Mono<MyThing> getMyThingMono() {
        return Mono.just(new MyThing());
    }
}

从另一个 Mono 内部返回时如何避免中断 WebFilter 中的请求流<object> &gt;?<div id="text_translate"><p> 例如,这是一些荒谬的代码</p><pre>@Component public final class ReCaptchaCheckWebFilter implements WebFilter { @Override public Mono&lt;Void&gt; filter(ServerWebExchange exchange, WebFilterChain chain) { return Mono.just("foo").map(s -&gt; chain.filter(exchange)).then(); } }</pre><p> 使用这个 webfiler,请求永远不会到达我的 Controller。 它确实返回 200 http 状态代码。</p><p> 如果我发现自己需要从另一个 Mono&lt;Object&gt; 内部返回,在将请求沿过滤器链转发到 controller 时,我该怎么做?</p></div></object> - How can avoid breaking request flow in WebFilter when returning from inside another Mono<Object>>?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从将返回 mono 的可调用对象构建 mono? - How do I build a mono from a callable that would return a mono? 从另一个 Mono 内部返回时如何避免中断 WebFilter 中的请求流<object> &gt;?<div id="text_translate"><p> 例如,这是一些荒谬的代码</p><pre>@Component public final class ReCaptchaCheckWebFilter implements WebFilter { @Override public Mono&lt;Void&gt; filter(ServerWebExchange exchange, WebFilterChain chain) { return Mono.just("foo").map(s -&gt; chain.filter(exchange)).then(); } }</pre><p> 使用这个 webfiler,请求永远不会到达我的 Controller。 它确实返回 200 http 状态代码。</p><p> 如果我发现自己需要从另一个 Mono&lt;Object&gt; 内部返回,在将请求沿过滤器链转发到 controller 时,我该怎么做?</p></div></object> - How can avoid breaking request flow in WebFilter when returning from inside another Mono<Object>>? 如何<B><A>在出错时</a>从 Mono返回 Mono <B><A>?</a> - How to return Mono<B> from Mono<A> on error? 如何退回 Mono <list<object> &gt; 变成 HashMap? </list<object> - How do I return a Mono<List<Object>> into a HashMap? Flux 的 onComplete 执行后如何返回 Mono? - How do I return a Mono after Flux's onComplete is executed? 如何从for循环中返回值? - How do I return a value from within a for loop? 如何使用来自 Mono 的数据添加延迟? - How do I add a delay with data from the Mono? 如何为正在编写反应器上下文的 WebFilter 编写单元测试? 具体来说,我要模拟什么以及如何模拟? - How to write unit test for a WebFilter that is writing a Reactor Context? Specifically, what and how do I mock? 如何在要返回的方法中从lambda返回值? - How do I return a value from a lambda within the method I want to return on? 如何使用返回 Mono 的生成包装调用创建 Flux - How do I create a Flux using generate wrapping calls that return a Mono
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM