[英]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());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.