Reactive Java
WebClient.exchangeToMono
Choose decoding based on response metadata and release unused body after termination. Source-reviewed Reactor or WebFlux example, NOT RUN.
Formal Syntax & Structure
java syntaxjava
request.exchangeToMono(response -> response.bodyToMono(Item.class))Working Example
Example Snippetjava
package lab;import java.time.Duration;import org.springframework.http.HttpStatus;import org.springframework.web.reactive.function.client.WebClient;import reactor.core.publisher.Mono;public class CatalogClient { // Inject a client with fixed trusted base URL, bounded codec size and connection timeout. private final WebClient client; public CatalogClient(WebClient client) { this.client = client; } public Mono<CatalogApplication.Item> read(String sku) { return client.get().uri("/items/{sku}", sku).exchangeToMono(response -> { if (response.statusCode().equals(HttpStatus.NOT_FOUND)) return Mono.empty(); if (response.statusCode().is2xxSuccessful()) return response.bodyToMono(CatalogApplication.Item.class); return response.createException().flatMap(Mono::error); }).timeout(Duration.ofSeconds(2)); }}