Home > Net >  How to compine two webclient mono calls into one mono<ResponseEntity<>> call
How to compine two webclient mono calls into one mono<ResponseEntity<>> call

Time:10-13

I have two calls a() and b() and both returnMono<String>, the string is a json with different structures. I also have two methods that extract data from each json string. I want to combine the two into one object, lets call it AA. What I started to write was

val aRes = a()
val bRes = b()
return aRes
       .map {
          str -> 
             AA(
              aa = extractAAFromARes(str)
              bb = extractBBFromBRes(bRes.block()!!) 
              )
       }
       .map { body -> ResponseEntity.ok().body(body) }
       .toMono() 

I get an error on bb = extractBBFromBRes(bRes.block()!!)

ava.lang.IllegalStateException: block()/blockFirst()/blockLast() are blocking, which is not 
supported in thread reactor-http-epoll-2

how can I combine the two calls without getting the blockin error?

Thank you

es

CodePudding user response:

You should not block reactive flow. Instead of it use operators like Mono.when(aa, bb) if you just want to wait until both publishers resolve or Mono.zip(aa, bb) that would return Mono<Tuple2<T1, T2>> in case you need results downstream. You could also consider Mono.zipDelayError in case you need special error handling.

  • Related