0Pricing
Spring Boot 4 Microservices & REST APIs · درس

WebClient للاستدعاءات التفاعلية

استدعِ الخدمات الأخرى بطريقة تفاعلية

WebClient للاستدعاءات التفاعلية درس مجاني في Spring Boot 4 Microservices & REST APIs على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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 / Flux responses
  • 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/bodyToFlux for responses
  • onStatus for error handling
  • timeout and retry for resilience

الأسئلة الشائعة

هل درس «WebClient للاستدعاءات التفاعلية» مجاني؟

نعم — نص درس «WebClient للاستدعاءات التفاعلية» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Spring Boot 4 Microservices & REST APIs، انتقل إلى CoddyKit PRO. تتضمن دورة Spring Boot 4 Microservices & REST APIs 4 دروس في المجموع.

ماذا ستتعلم في «WebClient للاستدعاءات التفاعلية»؟

استدعِ الخدمات الأخرى بطريقة تفاعلية تتمرن على Spring Boot 4 Microservices & REST APIs مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Spring Boot 4 Microservices & REST APIs؟

لا تُشترط خبرة سابقة. Spring Boot 4 Microservices & REST APIs على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.

كم من الوقت يستغرق درس «WebClient للاستدعاءات التفاعلية»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Spring Boot 4 Microservices & REST APIs هذا؟

نعم. كل درس في Spring Boot 4 Microservices & REST APIs يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. أساسيات Mono وFlux
  2. وحدات التحكم التفاعلية
  3. WebClient للاستدعاءات التفاعلية
  4. الضغط العكسي والعوامل
← العودة إلى Spring Boot 4 Microservices & REST APIs