Spring WebFlux และแกนหลักของ Reactor
สร้าง REST API เชิงรีแอ็กทีฟโดยใช้ Spring WebFlux พร้อมทำงานกับ `Mono` และ `Flux` จากแกนหลักของ Reactor
Spring WebFlux และแกนหลักของ Reactor เป็นบทเรียน Spring Boot 4 Complete Guide ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Spring Boot 4 Complete Guide และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Spring Boot 4 Complete Guide มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Reactive Web with Spring WebFlux
Welcome to Spring WebFlux! This framework is Spring's answer to building reactive, non-blocking web applications.
Unlike traditional Spring MVC which uses a servlet-based, blocking model, WebFlux is designed for concurrency and high throughput with fewer threads.
It's perfect for microservices and applications needing to handle many concurrent connections efficiently.
Diving into Reactor Core
At the heart of Spring WebFlux lies Reactor Core. Reactor is a reactive programming library that implements the Reactive Streams specification.
It provides two key types for handling asynchronous data streams:
Mono: For sequences of 0 or 1 item.Flux: For sequences of 0 to N items.
These types represent publishers that emit data, which subscribers then consume.
`Mono`: Zero or One Element
A Mono is a specialized Publisher that can emit at most one item, or complete without emitting any item, or emit an error.
Think of it as an asynchronous container for a single value. It's ideal for operations that return a single result, like fetching a user by ID or performing a single update.
Your First `Mono`
Let's see Mono in action. We create a Mono and then subscribe() to it. The subscribe() method triggers the data flow.
import reactor.core.publisher.Mono;
public class Main {
public static void main(String[] args) {
Mono<String> greetingMono = Mono.just("Hello, Mono!");
// Subscribe to the Mono to consume the data
greetingMono.subscribe(
data -> System.out.println(data), // onNext consumer
error -> System.err.println("Error: " + error), // onError consumer
() -> System.out.println("Mono completed.") // onComplete callback
);
}
}`Flux`: Zero to Many Elements
A Flux is a Publisher that can emit 0 to N items over time, and then optionally terminate with a completion signal or an error.
It's perfect for handling streams of data, like a list of products, real-time events, or database query results that return multiple rows.
Your First `Flux`
Here's an example of creating a Flux from a list of items. Just like Mono, you need to subscribe() to start the emission of data.
import reactor.core.publisher.Flux;
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> fruits = Arrays.asList("Apple", "Banana", "Cherry");
Flux<String> fruitFlux = Flux.fromIterable(fruits);
// Subscribe to the Flux to consume the data stream
fruitFlux.subscribe(
data -> System.out.println(data), // onNext consumer for each item
error -> System.err.println("Error: " + error), // onError consumer
() -> System.out.println("Flux completed.") // onComplete callback
);
}
}Building Reactive Endpoints
Spring WebFlux seamlessly integrates with Reactor Core's Mono and Flux. You can define your REST endpoints to return these reactive types.
When a WebFlux controller method returns a Mono or Flux, Spring handles the subscription and streaming of data automatically, allowing for non-blocking I/O.
Endpoint Returning `Mono`
Here's how you'd create a simple WebFlux endpoint that returns a Mono<String>. This endpoint would respond with a single string asynchronously.
Note: This code snippet is part of a Spring Boot application and cannot be run in isolation.
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;
@RestController
public class HelloController {
@GetMapping("/hello-mono")
public Mono<String> getHelloMono() {
// Simulate a non-blocking operation that returns a single greeting
return Mono.just("Hello from Mono endpoint!");
}
}Endpoint Returning `Flux`
For endpoints that need to stream multiple items, you can return a Flux<T>. WebFlux will handle sending these items as they become available, often using Server-Sent Events (SSE).
Note: This code snippet is part of a Spring Boot application and cannot be run in isolation.
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import java.time.Duration;
@RestController
public class HelloController {
@GetMapping(value = "/hello-flux", produces = "text/event-stream")
public Flux<String> getHelloFlux() {
// Emit 5 items, one every second
return Flux.interval(Duration.ofSeconds(1))
.map(sequence -> "Item " + sequence + " from Flux!")
.take(5);
}
}Check Your Knowledge
Let's test your understanding of Spring WebFlux and Reactor Core.
Recap: WebFlux & Reactor Core
In this lesson, you've learned about the core components of reactive programming with Spring Boot:
- Spring WebFlux: The non-blocking web framework.
- Reactor Core: The underlying library providing reactive types.
Mono: For handling 0 or 1 data item.Flux: For handling 0 to N data items.
You also saw how to define reactive endpoints in WebFlux controllers by returning Mono and Flux.
คำถามที่พบบ่อย
บทเรียน “Spring WebFlux และแกนหลักของ Reactor” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “Spring WebFlux และแกนหลักของ Reactor” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Spring Boot 4 Complete Guide ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Spring Boot 4 Complete Guide มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “Spring WebFlux และแกนหลักของ Reactor”
สร้าง REST API เชิงรีแอ็กทีฟโดยใช้ Spring WebFlux พร้อมทำงานกับ `Mono` และ `Flux` จากแกนหลักของ Reactor คุณปฏิบัติ Spring Boot 4 Complete Guide ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Spring Boot 4 Complete Guide หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Spring Boot 4 Complete Guide บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “Spring WebFlux และแกนหลักของ Reactor” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Spring Boot 4 Complete Guide นี้ได้ไหม
ได้ บทเรียน Spring Boot 4 Complete Guide ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- บทนำสู่การเขียนโปรแกรมเชิงรีแอ็กทีฟ
- Spring WebFlux และแกนหลักของ Reactor
- การเข้าถึงและผสานรวมข้อมูลเชิงรีแอ็กทีฟ
- การควบคุมแรงดันย้อนกลับและการจัดการข้อผิดพลาดในสตรีมแบบรีแอกทีฟ