Once our WebClient is configured for a specific baseUrl, we can start performing HTTP requests.
As the internal WebClient architecture is designed for reactive and non-blocking applications, we either have to call .block() or rewrite our codebase to accept Mono
A simple sync HTTP GET request with our previously configured WebClient looks like the following:
xxxxxxxxxx
@Service
public class SimpleApiClient {
private final WebClient defaultWebClient;
// inject the configured WebClient @Bean from the configuration above
public SimpleApiClient(WebClient defaultWebClient) {
this.defaultWebClient = defaultWebClient;
}
public JsonNode getTodoFromAPI() {
return this.defaultWebClient
.get()
.uri("/todos/1")
.retrieve()
.bodyToMono(JsonNode.class)
.block();
}
}