19

I've been trying to follow the simplest tutorials out there for how to use WebClient, which I understand to be the next greatest thing compared to RestTemplate.

For example, https://www.baeldung.com/spring-5-webclient#4-getting-a-response

enter image description here

So when I try to do the same thing with https://petstore.swagger.io/v2/pet/findByStatus?status=available which is supposed to return some json,

WebClient webClient = WebClient.create();
webClient.get().uri("https://petstore.swagger.io/v2/pet/findByStatus?status=available").exchange().block();

enter image description here

I have absolutely no idea how to proceed from the resultant DefaultClientResponse object. It shouldn't be this convoluted to arrive at the physical response body, but I digress.

How do I get the response body with the code I provided?

2
  • "When I try the same thing", you are not doing the same thing? You aren't doing anything to the client response e.g. mapping it, or calling the bodyToMono that will map the body to a type... Jul 9, 2020 at 20:25
  • wow.... i figured it out
    – notacorn
    Jul 9, 2020 at 20:28

2 Answers 2

32

In the form you currently have it, and explaining the behaviour..

WebClient webClient = WebClient.create();
webClient.get()
         .uri("https://petstore.swagger.io/v2/pet/findByStatus?status=available")
         .exchange()
         .block();

the block() starts the request by internally synchronously subscribing to the Mono, and returns the resulting ClientResponse. You could also handle this asynchronously by calling subscribe() on the Mono returned by the exchange() method, instead of block().

In this current form, after the block() you now have all the metadata (ie. from the response header) about the response in a ClientResponse object, including the success status. This does not mean that the response body has finished coming through. If you don't care about the response payload, you could confirm the success and leave it at that.

If you further want to look at the response body, you need to convert the response body stream into some class. A this point you can decide whether you want to read everything into a single Mono with bodyToMono or into a stream of objects (Flux) with bodyToFlux, such as in the case where the response is a JSON array that can be parsed into individual separate Java objects.

However, in your case, you just want to see the JSON as-is. So converting to a String is sufficient. You would just use bodyToMono which would return a Mono object.

WebClient webClient = WebClient.create();
String responseJson = webClient.get()
                               .uri("https://petstore.swagger.io/v2/pet/findByStatus?status=available")
                               .exchange()
                               .block()
                               .bodyToMono(String.class)
                               .block();

Here you use block() to wait for the response payload to arrive and be parsed into a String, but you could also subscribe to the Mono to receive it reactively when it is complete.

One thing to note is that retrieve() can be used instead of exchange() to shortcut the ClientResponse. In this case you let default behavior handle error responses. Using exchange() puts all the responsibility on the application for responding to error responses on the ClientResponse. Read more in the Javadoc. The retrieve() version would look as follows. No need to block() as you only care about the response data.

WebClient webClient = WebClient.create();
String responseJson = webClient.get()
                               .uri("https://petstore.swagger.io/v2/pet/findByStatus?status=available")
                               .retrieve()
                               .bodyToMono(String.class)
                               .block();
2

Here is how you make a request with RestTemplate

String json = new RestTemplate()
    .getForEntity("https://petstore.swagger.io/v2/pet/findByStatus?status=available")
    .getBody();

Here is how you make a request with requests

import requests

json = requests.get("https://petstore.swagger.io/v2/pet/findByStatus?status=available")
    .content

Here is how you make a request with WebClient

String json = WebClient.create()
    .get()
    .uri("https://petstore.swagger.io/v2/pet/findByStatus?status=available")
    .exchange()
    .block()
    .bodyToMono(String.class)
    .block();
4
  • 2
    because requests is written in javascript without a type system hence getForEntity, also requests is inherently blocking, which WebClient per default is not. Your comparison between RestTemplate and request is fair, because they are both blocking clients. if you wish to do a fair comparison you should compare WebClient to something like Fetch or Axios that returns a Promise and you'll notice that javascript is quite "ugly" as well. You are comparing an apple to a ferrari. Jul 9, 2020 at 22:33
  • 2
    so if they are inherently different categories/types, then why does everything I read say RestTemplate is being depracated in favor of WebClient? @ThomasAndolf why are we getting rid of the spring blocking client?
    – notacorn
    Jul 10, 2020 at 0:27
  • 2
    Because there is no sense to maintain 2 clients, its twice the amount of work, When WebClient can act as RestTemplate, but RestTemplate cant act as WebClient Jul 10, 2020 at 6:33
  • 2
    also, calling block twice is not necessary, the first one can be removed. Jul 10, 2020 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.