Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

read request body in webflux

public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
    Flux<DataBuffer> body = exchange.getRequest().getBody();
    //Return different according to body content...
    if (condition) {
        return chain.filter(exchange);
    } else {
        return Mono.empty();
    }
}  

How do I get the body of the request to do some custom judgments in spring-webflux with spring 5?

like image 850
Kerwin Bryant Avatar asked Nov 08 '22 03:11

Kerwin Bryant


1 Answers

Your question is not entirely clear. I assume your doubt is what you put in the code snippet comments.

There are probably different ways to achieve what you want. One simple way to do it is by using a flatMap operator. Somewhat as follows:

public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
    Flux<DataBuffer> body = exchange.getRequest().getBody()
       .flatMap(data -> {
           if (condition) {
              return chain.filter(exchange);
           } 
           return Mono.empty();
       });
  //...
}  
like image 168
Edwin Dalorzo Avatar answered Nov 15 '22 14:11

Edwin Dalorzo