Sending requests and retrieving responses

The retrieve() method is a simple method, using which the response body can be retrieved. If you want to have more control over returned responses, the exchange() method can be used to retrieve the response. We have used both of the methods in our sample application; the code snippets for the two methods in the WebClientTestImpl class are as follows:

@Override
public Mono<Movie> getMovieById(Long id)
return this.webClient.get().uri("/{id}", id)
.retrieve().bodyToMono(Movie.class);
}
@Override
public Mono<Movie> saveMovie(Movie movie) {
return webClient.post().uri("/")
.body(BodyInserters.fromObject(movie))
.exchange().flatMap( clientResponse ->
clientResponse.bodyToMono(Movie.class) );
}

In the first method, we execute a GET method on the URI http://localhost:8080/api/movie/{id}, use the retrieve() method, and then convert into Mono.

In the second method, we execute a POST method on the URL http://localhost:8080/api/movie, use the exchange() method, and use the flatMap() method to create the response.

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

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