Ananya Antony
Ananya Antony

Reputation: 358

Rest Service Post Calls Not working with Spring Webclient

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?

Upvotes: 0

Views: 4965

Answers (1)

JK.Lee
JK.Lee

Reputation: 261

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

Upvotes: 1

Related Questions