Reactive Java with spring webflux and reactor

Using a reactive customer http

Now we create an endpoint that accepts the ID parameter. We will use the spring reactive customer HTTP and API ice and fire to ask OA Grade An ID -based character. Then, send the characters data back to the user. New announcement apiChain() Method and its import here:


import org.springframework.web.reactive.function.client.WebClient;

@GetMapping("character/{id}")
  public Mono getCharacterData(@PathVariable String id) {
    WebClient client = WebClient.create("https://anapioficeandfire.com/api/characters/");
    return client.get()
      .uri("/{id}", id)
      .retrieve()
      .bodyToMono(String.class)
      .map(response -> "Character data: " + response);
  }

If you navigate localhost:8080/character/148You will get biographical information for who is obviously the best character in Grade.

This example works by accepting the road ID and its use to submit an application WebClient Class. In this case we create an instance for our request, but you can create a WebClient With the URL basic address and then use it repeatedly with many ways. We put ID on the road and then we call retrieve Follows bodyToMono()Which transforms the response to a Mono. Remember that all of this remains non -blocking and asynchronous, so the code waiting for the API responsibility does not block the thread. Finelly, we used map() Formulate the answer back to the user.

Leave a Comment