如何使用WebClient执行同步请求?

Spring文档指出,即使我们要执行同步http调用,也必须从RestTemplate切换到WebClient

现在我有以下代码:

  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;

当然可以在这里使用CountdownLatch,但是它似乎在滥用API。

如何执行同步请求?

huazhua88225 回答:如何使用WebClient执行同步请求?

有效:

webClient.post()
         .bodyValue(myDto)
         .retrieve()
         .toEntity(MyDto.class)
         .block(); // <-- This line makes trick
本文链接:https://www.f2er.com/3144036.html

大家都在问