Creating a WebClient instance

A WebClient instance can be created by using the create() method, or by using the builder() method. In our sample, we have used the builder() method, as follows:

@Service
public class WebClientTestImpl implements WebClientTestInterface {
private final WebClient webClient;
public WebClientTestImpl(WebClient.Builder webClientBuilder) {
this.webClient = webClientBuilder.defaultHeader(HttpHeaders.ACCEPT,
MediaType.APPLICATION_JSON_VALUE)
.baseUrl("http://localhost:8080/api/movie").build();
}
//...Other methods
}

We will be using all of the endpoints that we have created earlier in our base Spring WebFlux project, and will be accessing them using the WebClient.

Use the create() method to create an instance of WebClient, as follows:

WebClient webClient = WebClient.create();

If you have a base URL, WebClient can be created as follows:

WebClient webClient = WebClient.create("http://localhost:8080/api/movie");

The builder() method provides bunch of utility methods, such as filters, setting headers, setting cookies, and so on. In our example, we have set some default headers and have also set the base URL.

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

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