Reactive WebClient

Spring WebFlux includes a reactive client named WebClient, enabling us to perform HTTP requests in a non-blocking manner and to use reactive streams. WebClient can be used as an alternative to RestTemplate, which is used more traditionally. WebClient exposes reactive ClientHttpRequest and ClientHttpResponse objects. The bodies of these objects consist of reactive Flux<DataBuffer>, as opposed to traditional blocking stream implementation (InputStream and OutputStream).

Create an instance of WebClient, perform a request, and then handle the response. The following is a code snippet showing the WebClient usage:

WebClient client = WebClient.create("http://any-domain.com");
Mono<Movie> movie = client.get()
.url("/movie/{id}", 1L)
.accept(APPLICATION_JSON)
.exchange(request)
.then(response -> response.bodyToMono(Movie.class));

WebClient can be used from within both Spring MVC and Spring WebFlux web applications. RestTemplate usage can quite easily be swapped with WebClient, making use of the reactive advantages it provides.

In our sample project, we will cover the concepts and functionality of WebClient, using an example.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.177.125