samedi 25 mai 2019

Spring Webclient does not return data but RestTemplate does

I am making a very straightforward call to a Restful web service. I started off by using a Spring 5 asynchronous Webclient. Unfortunately, that call does not return the data. When I use a RestTemplate instead, the data is returned. Here is the code the Webclient code, I am expecting a JSON string:

   WebClient webClient = WebClient.create("http://localhost/current/jobs")

     Mono<String> result=  webClient.get()

    .retrieve()

    .bodyToMono(String.class)

     result.block(Duration.ofSeconds(5L))

     result.subscribe{it -> println "Data is ${it}"}

This code, on the other hand, retrieves the data successfully:

    RestTemplate rt=new RestTemplate()

    Object obj= rt.getForObject("http://localhost/current/jobs", String.class)

Does the Webclient not work on synchronous code. I was hoping to use the WebClient but it is hard to decipher what's going on. I also, replace Mpno with Flux but still had no success with it either. I tried exchange() etc but still failed to return data. Please help.




Aucun commentaire:

Enregistrer un commentaire