0Pricing
Spring Boot 4 Microservices & REST APIs · 강의

리액티브 컨트롤러

WebFlux로 비차단 엔드포인트를 만들어 보세요.

리액티브 컨트롤러은(는) CoddyKit의 무료 Spring Boot 4 Microservices & REST APIs 강의입니다. 이것은 4개 중 2번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 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:

  • @RestController methods return Mono or Flux
  • Never call block() inside a handler
  • Use ResponseEntity in a Mono for dynamic status
  • Stream with text/event-stream

자주 묻는 질문

“리액티브 컨트롤러” 강의는 무료인가요?

네 — “리액티브 컨트롤러” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Spring Boot 4 Microservices & REST APIs 강의 전체를 잠금 해제할 수 있습니다. Spring Boot 4 Microservices & REST APIs 강의에는 총 4개의 강의가 포함되어 있습니다.

“리액티브 컨트롤러”에서 뭘 배우나요?

WebFlux로 비차단 엔드포인트를 만들어 보세요. 브라우저에서 직접 실행하는 실습 코드로 Spring Boot 4 Microservices & REST APIs을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Spring Boot 4 Microservices & REST APIs을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Spring Boot 4 Microservices & REST APIs은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 2번째 강의입니다.

“리액티브 컨트롤러” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 Spring Boot 4 Microservices & REST APIs 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 Spring Boot 4 Microservices & REST APIs 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. Mono와 Flux 기초
  2. 리액티브 컨트롤러
  3. 리액티브 호출을 위한 WebClient
  4. 백프레셔와 연산자
← Spring Boot 4 Microservices & REST APIs(으)로 돌아가기