リアクティブな呼び出しにWebClientを使う
他のサービスをリアクティブに呼び出します
「リアクティブな呼び出しにWebClientを使う」はCoddyKit上の無料Spring Boot 4 Microservices & REST APIsレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはSpring Boot 4 Microservices & REST APIs学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Spring Boot 4 Microservices & REST APIsコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
What is WebClient
WebClient is the non-blocking, reactive HTTP client in Spring WebFlux. It replaces the blocking RestTemplate for reactive apps.
- Returns
Mono/Fluxresponses - Fully non-blocking on the Netty event loop
Building a WebClient
Create an instance with a base URL using the builder.
WebClient client = WebClient.builder()
.baseUrl("https://api.example.com")
.build();A Simple GET
Chain get(), uri(), retrieve(), then convert the body to a publisher.
Mono<User> user = client.get()
.uri("/users/{id}", 1)
.retrieve()
.bodyToMono(User.class);
user.subscribe(System.out::println);Fetching a Collection
Use bodyToFlux when the response is a JSON array.
Flux<User> users = client.get()
.uri("/users")
.retrieve()
.bodyToFlux(User.class);Sending a POST Body
Use post() and bodyValue() to send a request body.
Mono<User> created = client.post()
.uri("/users")
.bodyValue(new User("Alice"))
.retrieve()
.bodyToMono(User.class);Adding Headers
Attach headers per request or globally on the builder.
Mono<String> result = client.get()
.uri("/data")
.header("Authorization", "Bearer token123")
.retrieve()
.bodyToMono(String.class);Handling Error Statuses
retrieve() throws on 4xx/5xx by default. Customize with onStatus.
Mono<User> user = client.get()
.uri("/users/{id}", 99)
.retrieve()
.onStatus(HttpStatusCode::is4xxClientError,
resp -> Mono.error(new RuntimeException("Not found")))
.bodyToMono(User.class);Composing Calls with flatMap
Because results are reactive, you can chain dependent HTTP calls without blocking.
Mono<Order> order = client.get()
.uri("/users/{id}", 1)
.retrieve()
.bodyToMono(User.class)
.flatMap(u -> client.get()
.uri("/orders?userId={id}", u.getId())
.retrieve()
.bodyToMono(Order.class));Timeouts
Apply a timeout with the timeout operator so slow upstreams fail fast.
Mono<User> user = client.get()
.uri("/users/1")
.retrieve()
.bodyToMono(User.class)
.timeout(Duration.ofSeconds(3));Retrying on Failure
Use the retry operator to re-subscribe on error a fixed number of times.
Mono<User> user = client.get()
.uri("/users/1")
.retrieve()
.bodyToMono(User.class)
.retry(2);Registering as a Bean
Configure a shared WebClient bean so it can be injected anywhere.
@Bean
public WebClient webClient() {
return WebClient.builder()
.baseUrl("https://api.example.com")
.build();
}Quick Check
Test your understanding of WebClient.
Recap
You learned to make non-blocking HTTP calls with WebClient:
- Build with
WebClient.builder().baseUrl(...) retrieve().bodyToMono/bodyToFluxfor responsesonStatusfor error handlingtimeoutandretryfor resilience
よくある質問
「リアクティブな呼び出しにWebClientを使う」レッスンは無料ですか?
はい。「リアクティブな呼び出しにWebClientを使う」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Spring Boot 4 Microservices & REST APIsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Spring Boot 4 Microservices & REST APIsコースには全4レッスンが含まれています。
「リアクティブな呼び出しにWebClientを使う」で何を学びますか?
他のサービスをリアクティブに呼び出します ブラウザで直接実行するハンズオンコードでSpring Boot 4 Microservices & REST APIsを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Spring Boot 4 Microservices & REST APIsを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのSpring Boot 4 Microservices & REST APIsは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「リアクティブな呼び出しにWebClientを使う」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このSpring Boot 4 Microservices & REST APIsレッスンでコードを書いて実行できますか?
はい。すべてのSpring Boot 4 Microservices & REST APIsレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- MonoとFluxの基礎
- リアクティブコントローラー
- リアクティブな呼び出しにWebClientを使う
- バックプレッシャーとオペレーター