WebClient สำหรับการเรียกแบบรีแอกทีฟ
เรียกใช้บริการอื่นแบบรีแอกทีฟ
WebClient สำหรับการเรียกแบบรีแอกทีฟ เป็นบทเรียน Spring Boot 4 Microservices & REST APIs ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 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/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 สำหรับการเรียกแบบรีแอกทีฟ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Spring Boot 4 Microservices & REST APIs ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Spring Boot 4 Microservices & REST APIs มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “WebClient สำหรับการเรียกแบบรีแอกทีฟ”
เรียกใช้บริการอื่นแบบรีแอกทีฟ คุณปฏิบัติ Spring Boot 4 Microservices & REST APIs ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 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 ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- พื้นฐาน Mono และ Flux
- คอนโทรลเลอร์แบบรีแอกทีฟ
- WebClient สำหรับการเรียกแบบรีแอกทีฟ
- แรงดันย้อนกลับและตัวดำเนินการ