Webclient
When to Use WebClient
When building reactive applications (e.g., using Spring WebFlux).
When you need non-blocking I/O for better scalability and performance.
When working with streaming APIs or handling large datasets.
When you want to use functional programming for HTTP requests.
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
We can use reactive extreame to do the handling . We have certain methods like doOnError, doOnStatus, onErrorResume etc, using them we can define how we want to handle it.
Log request and response of requests
To log request and reponse in webclient we can utilize the filters.
We can use ExchangeFilterFunction that have methods like ofRequestProcessor and ofResponseProcessor .
Create log filters for request and response and attach in configuration.
Multiple web clients bean
Yes we can have multiple webclients bean present in the system. We need to create multiple beans with different bean names , then whenever required can use @Qualifier to inject the bean required.
Custom Headers and Query Parameters
Add custom headers and query parameters dynamically.
Parallel requests
We can utilize zip method from reactive streams for this .
Streaming Data
Can be used to stream data from an API (e.g., Server-Sent Events or large JSON arrays).
Caching
Implement caching for responses.
Reactive Caching with Redis
Use Redis for reactive caching of responses.
File Upload
Upload a file using WebClient with multipart/form-data.
File Download
Download a file and save it to the local filesystem.
Custom Serialization/Deserialisation
Use custom serialization/deserialisation for requests and responses.
Custom SSL Configuration
Configure custom SSL settings .
Custom Metrics
Add custom metrics for monitoring WebClient requests.
Last updated