0

I need to invoke a rest service asynchronously and I thought of using spring reactive's webclient instead of the AsyncRestTemplate. However my url is not getting invoked at all with the below code.

Mono<Test> asyncResponse = webClientBuilder.build().post().uri(url).contentType(MediaType.APPLICATION_JSON)
                .header("h1", h1).header("h2", h2)
                .body(BodyInserters.fromObject(request))
                .retrieve().bodyToMono(Test.class);

However if I do the same synchronously everything works fine.

webClientBuilder.build().post().uri(url).contentType(MediaType.APPLICATION_JSON)
                .header("h1", h1).header("h2", h2)
                .body(BodyInserters.fromObject(request))
                .exchange();`

What am I doing wrong?

Ananya Antony
  • 338
  • 3
  • 15

1 Answers1

1

exchange doesn't mean synchronous. It responds Mono. You need to subscribe() or block() your stream somewhere.

Difference with exchange and retrieve is : They differ in return types; the exchange method provides a ClientResponse along with its status, headers while the retrieve method is the shortest path to fetching a body directly. you can refer this

JK.Lee
  • 261
  • 3
  • 9