Home > Software engineering >  Call Mono after first Mono is done and return result of first Mono
Call Mono after first Mono is done and return result of first Mono

Time:06-22

I want to perform two business operations, in a Webflux environment, in a way that the second operation happens only after the first one is succesfull. After the second one is done, I want to return the result of the first operation. The second operation calls a org.springframework.web.reactive.function.client.WebClient. This is what I have tried until now:

public Mono<ResponseEntity<Resource>> callOperations(){
   return service.operation1()
          .flatMap(resource -> {
                   service.operation2();
                   return resource;
           })
          .map(ResponseEntity::ok);

}

I also tried with then and subscribe but I can't get the webclient to perform the call and return the result of service.operation1. What must I do?

CodePudding user response:

You need to construct a flow using reactive operators and let WebFlux subscribe to it. In your snippet there is no subscription to service.operation2()

public Mono<ResponseEntity<Resource>> callOperations(){
   return service.operation1()
          .flatMap(resource -> {
                   return service.operation2()
                       .thenReturn(resource);
           })
          ...

}
  • Related