[SpringCloudGateway]: How to call REST API using Spring Webclient in global post filter

  Kiến thức lập trình

Spring Cloud Gateway with WebFlux: 4.1.0

We have implemented couple of custom Global pre and post filters in our Spring Cloud Gateway application. We are using Spring WebClient utility to make call to REST APIs exposed from other microservices. All the REST. APIs that are called from pre-filters are working fine, however the post-filter are unable to call these APIs.

Following is the code snippet of custom post filter that makes rest api call:

public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
    return chain.filter(exchange).then(Mono.fromRunnable(() -> {
       ServerHttpResponse response = exchange.getResponse();
       if(response.getStatusCode().is2xxSuccessful()) {
          log.debug("Making call to rest api using webclient tool");
          WebClient.RequestHeadersSpec requestPostHandler = webClientPostHandler.post()
            .bodyValue("req body....");
          Mono<ClientResponse> responsePostHandler = requestPostHandler.exchange();
          return responsePostHandler.flatMap(postHandlerResponse -> {
              return postHandlerResponse.bodyToMono(void.class).flatMap(postHandlerBody -> {
                  if (!postHandlerBody.statusCode().equals(HttpStatus.OK)) {
                      log.debug("Handling response from rest api call");
                      return this.handleExceptionCases(exchange, postHandlerBody.toString(), postHandlerResponse.statusCode());

First log statement is getting printed however, the logger statement inside webclient post call block is not getting printed. No request is sent to the REST API by the webclient.

We searched over the internet for the solution and could find one of the solutions as to change Mono.fromRunnable to Mono.defer.
Is there any feasible solution to make it work with Mono.fromRunnable method? We already have couple of post-filters which are now developed using this method in base class. Changing it now will require lots of regression tests to confirm the behavior.
Any help is highly appreciated.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website