Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use WebClient to execute synchronous request?

Spring documentation states that we have to switch from RestTemplate to WebClient even if we want to execute synchronous http call.

For now I have following code:

  Mono<ResponseEntity<PdResponseDto>> responseEntityMono = webClient.post()
                .bodyValue(myDto)
                .retrieve()
                .toEntity(MyDto.class);
        responseEntityMono.subscribe(resp -> log.info("Response is {}", resp));
   //I have to return response here
   // return resp;

Sure I could use CountdownLatch here but it looks like API misusing.

How could I execute synchronous request ?

like image 629
gstackoverflow Avatar asked Nov 07 '19 14:11

gstackoverflow


1 Answers

It works:

webClient.post()
         .bodyValue(myDto)
         .retrieve()
         .toEntity(MyDto.class)
         .block(); // <-- This line makes trick
like image 147
gstackoverflow Avatar answered Oct 16 '22 15:10

gstackoverflow