0Pricing
Spring Boot 4 Microservices & REST APIs · Lektion

WebClient für reaktive Aufrufe

Rufen Sie andere Services reaktiv auf.

WebClient für reaktive Aufrufe ist eine kostenlose Spring Boot 4 Microservices & REST APIs-Lektion auf CoddyKit. Dies ist Lektion 3 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Spring Boot 4 Microservices & REST APIs-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Spring Boot 4 Microservices & REST APIs-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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

Häufig gestellte Fragen

Ist die Lektion „WebClient für reaktive Aufrufe“ kostenlos?

Ja — der vollständige Text von „WebClient für reaktive Aufrufe“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Spring Boot 4 Microservices & REST APIs-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Spring Boot 4 Microservices & REST APIs-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „WebClient für reaktive Aufrufe“?

Rufen Sie andere Services reaktiv auf. Du übst Spring Boot 4 Microservices & REST APIs mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Spring Boot 4 Microservices & REST APIs zu starten?

Keine Vorkenntnisse erforderlich. Spring Boot 4 Microservices & REST APIs auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 3 von 4.

Wie lange dauert die Lektion „WebClient für reaktive Aufrufe“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Spring Boot 4 Microservices & REST APIs-Lektion Code schreiben und ausführen?

Ja. Jede Spring Boot 4 Microservices & REST APIs-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Grundlagen von Mono und Flux
  2. Reaktive Controller
  3. WebClient für reaktive Aufrufe
  4. Backpressure und Operatoren
← Zurück zu Spring Boot 4 Microservices & REST APIs