Webclient
When to Use WebClient
Dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>Sample Usages
// get example
public <T> Mono<T> get(String url, Class<T> responseType,String token) {
return webClient.get()
.uri(getUriBuilderURIFunction(url))
.header(MessageConst.AUTHORIZATION, MessageConst.BEARER +token)
.retrieve()
.bodyToMono(responseType)
.timeout(timeout)
.doOnSuccess(response -> log.info("Received successful response from GET {}", url))
.doOnError(error -> log.error("Error in GET request to {}: {}", url, error.getMessage()))
.retryWhen(
Retry.backoff(retry, Duration.ofSeconds(1)).filter(e->!e.getMessage().equals("401"))
.doBeforeRetry(retrySignal -> log.warn("Retrying GET request to {}", url))
)
.onErrorResume(this::handleError);
}
// post example
public <T> Mono<T> post(String url, Object body, Class<T> responseType,String token) {
return webClient.post().uri(getUriBuilderURIFunction(url))
.header(MessageConst.AUTHORIZATION, MessageConst.BEARER +token)
.bodyValue(body)
.retrieve()
.bodyToMono(responseType)
.timeout(timeout)
.doOnSuccess(response -> log.info("Received successful response from POST {}", url))
.doOnError(error -> log.error("Error in POST request to {}: {}", url, error.getMessage()))
.onErrorResume(this::handleError);
}
Exception handling
Log request and response of requests
Multiple web clients bean
Custom Headers and Query Parameters
Parallel requests
Streaming Data
Caching
Reactive Caching with Redis
File Upload
File Download
Custom Serialization/Deserialisation
Custom SSL Configuration
Custom Metrics
Last updated