คอนโทรลเลอร์แบบรีแอกทีฟ
สร้างปลายทางที่ไม่บล็อกด้วย WebFlux
คอนโทรลเลอร์แบบรีแอกทีฟ เป็นบทเรียน Spring Boot 4 Microservices & REST APIs ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Spring Boot 4 Microservices & REST APIs และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Spring Boot 4 Microservices & REST APIs มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Reactive Web Endpoints
With WebFlux you write @RestController classes just like in Spring MVC, but methods return reactive types instead of plain objects.
- Return
Mono<T>for a single resource - Return
Flux<T>for a collection or stream
Returning a Mono
A handler returning Mono resolves to a single JSON object. The framework subscribes for you.
@RestController
@RequestMapping("/users")
public class UserController {
@GetMapping("/{id}")
public Mono<User> getUser(@PathVariable String id) {
return userService.findById(id);
}
}Returning a Flux
A handler returning Flux serializes to a JSON array by default.
@GetMapping
public Flux<User> getAllUsers() {
return userService.findAll();
}No Blocking Allowed
Inside reactive handlers you must not call blocking code. Return the publisher and let the chain stay reactive.
// BAD: blocks the event loop
@GetMapping("/bad")
public User bad() {
return userService.findById("1").block();
}
// GOOD: stays reactive
@GetMapping("/good")
public Mono<User> good() {
return userService.findById("1");
}Path and Query Params
Binding annotations work the same as in MVC: @PathVariable, @RequestParam, and @RequestBody.
@GetMapping("/search")
public Flux<User> search(@RequestParam String name) {
return userService.findByName(name);
}Accepting a Reactive Body
A POST handler can accept a Mono request body, keeping the whole flow non-blocking.
@PostMapping
public Mono<User> create(@RequestBody Mono<User> body) {
return body.flatMap(userService::save);
}Setting Status Codes
Use @ResponseStatus or wrap results in ResponseEntity within a Mono.
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public Mono<User> create(@RequestBody Mono<User> body) {
return body.flatMap(userService::save);
}ResponseEntity in a Mono
To control headers and status dynamically, return Mono<ResponseEntity<T>>.
@GetMapping("/{id}")
public Mono<ResponseEntity<User>> getUser(@PathVariable String id) {
return userService.findById(id)
.map(ResponseEntity::ok)
.defaultIfEmpty(ResponseEntity.notFound().build());
}Streaming with Server-Sent Events
Set the content type to text/event-stream to stream a Flux to the client over time.
@GetMapping(value = "/stream",
produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<Long> stream() {
return Flux.interval(Duration.ofSeconds(1));
}Error Handling
Use onErrorResume to map errors to a fallback publisher, or a @ControllerAdvice for global handling.
@GetMapping("/{id}")
public Mono<User> getUser(@PathVariable String id) {
return userService.findById(id)
.onErrorResume(e -> Mono.error(
new ResponseStatusException(HttpStatus.NOT_FOUND)));
}The Netty Server
WebFlux runs on Netty by default — an event-loop server with a small fixed thread pool. This is why blocking calls are so harmful: blocking one thread stalls many requests.
Quick Check
Test your understanding of reactive controllers.
Recap
You learned how to build reactive endpoints:
@RestControllermethods returnMonoorFlux- Never call
block()inside a handler - Use
ResponseEntityin a Mono for dynamic status - Stream with
text/event-stream
คำถามที่พบบ่อย
บทเรียน “คอนโทรลเลอร์แบบรีแอกทีฟ” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “คอนโทรลเลอร์แบบรีแอกทีฟ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Spring Boot 4 Microservices & REST APIs ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Spring Boot 4 Microservices & REST APIs มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “คอนโทรลเลอร์แบบรีแอกทีฟ”
สร้างปลายทางที่ไม่บล็อกด้วย WebFlux คุณปฏิบัติ Spring Boot 4 Microservices & REST APIs ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Spring Boot 4 Microservices & REST APIs หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Spring Boot 4 Microservices & REST APIs บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “คอนโทรลเลอร์แบบรีแอกทีฟ” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Spring Boot 4 Microservices & REST APIs นี้ได้ไหม
ได้ บทเรียน Spring Boot 4 Microservices & REST APIs ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- พื้นฐาน Mono และ Flux
- คอนโทรลเลอร์แบบรีแอกทีฟ
- WebClient สำหรับการเรียกแบบรีแอกทีฟ
- แรงดันย้อนกลับและตัวดำเนินการ