Spring Webflux - pch8388/foo-board GitHub Wiki

Webflux 데이터가 없을 경우

  • MVC μ—μ„œ νŠΉμ • λŒ€μƒμ„ μ‘°νšŒν–ˆλŠ” 데, 없을 κ²½μš°μ—λŠ” Optional 의 orElseThrow() 와 같은 λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•˜μ—¬ μ˜ˆμ™Έλ₯Ό λ°œμƒμ‹œν‚€λŠ” 방법을 μ‚¬μš©ν•˜μ˜€μ—ˆλ‹€
  • Webflux μ—μ„œ Mono λ‚˜ Flux μ‚¬μš©μ‹œ 데이터가 μ—†λ‹€λ©΄ switchIfEmtpy λ₯Ό μ‚¬μš©ν•  수 μžˆλ‹€
return this.postRepository.findById(postId)
  .flatMap(post -> {
    post.updateContent(updatePostDto.getUpdateContent());
    return this.postRepository.save(post);
  })
  .map(ResponsePostDto::convertFromEntity)
  .switchIfEmpty(Mono.error(new IllegalArgumentException("잘λͺ»λœ post id")));
  • switchIfEmpty λŠ” Mono(ν˜Ήμ€ Flux) κ°€ λΉ„μ–΄μžˆμ„ 경우 λŒ€μ²΄ν•  Mono λ₯Ό μ •μ˜ν•  수 있게 ν•΄μ€€λ‹€ => 즉 Mono.error λ₯Ό λ°˜ν™˜ν•˜λ„λ‘ ν•˜λ©΄ μ˜ˆμ™Έμ²˜λ¦¬λ₯Ό ν•  수 있음