본문 바로가기
728x90
반응형

spring/spring webFlux5

[WebFlux] Mono에 담긴 List를 하나로 합치기 Spring WebClient를 통해 동일한 객체타입을 리턴하는 여러 외부 API를 호출하면서 Mono를 List로 담았는데, 각 API 호출 응답으로 받은 객체가 List여서 Flux.merge(monoList).collectList() 타입이 Mono이 돼버렸습니다. Mono가 아닌 Mono로 리턴할 수 있는 방법을 알아보겠습니다. 우선 아래 코드를 비교해 확인해 주세요. // AS-IS public Mono getList(...) { List monoList = new ArrayList(); Mono listMono1 = Mono.just(List.of(...)); monoList.add(listMono1); Mono listMono2 = Mono.just(List.of(...)); monoList.. 2023. 7. 13.
[Spring] WebMVC에서 WebFlux 사용 (with WebClient) Spring에는 두 가지 웹 프레임워크가 있습니다. WebMVC : 전통적인 멀티 스레드 기반의 웹 프레임워크 WebFlux : 리액티브 스택 기반의 웹 프레임워크 Spring WebMVC와 WebFlux는 공존할 수 없다고 생각했는데, Spring framework에서 제공하는 Http Client API로 RestTemplate 대신 WebClient를 사용하라고 권고하고 있어서 의문이 생겼습니다. 두 모듈의 공존이 가능한 것인가? Spring 문서를 확인해보면 "애플리케이션은 하나 또는 다른 모듈을 사용하거나 경우에 따라 두 모듈을 모두 사용할 수 있습니다(예: 반응형 WebClient가 포함된 Spring MVC 컨트롤러)."라고 써져 있습니다. 실제로 spring-boot-starter-web,.. 2023. 7. 13.
[webFlux] SwitchIfEmpty가 항상 호출되는 이슈 해결 (Lazy Evaluation) webflux에서 if else문으로 switchIfEmpty를 주로 사용하는데, empty가 아닌 경우에도 switchIfEmpty를 항상 호출합니다. 왜 그러는 것인지, 어떻게 하면 호출을 안 하게 할 수 있는지 알아보겠습니다. 1: String str = "str"; 2: Mono.just(str) 3: .map(s -> { 4: System.out.println(s); 5: return s; 6: }) 7: .switchIfEmpty({ 8: System.out.println("defaultStr"); 9: return Mono.just("default"); 10: }) 11: .subscribe(); 위 코드를 보면 str이 비어 있지 않으니 문자열 "str"만 출력할 것이라고 생각하겠지만, 실.. 2022. 12. 20.
[SpringBoot] Reactive RESTful Web Service 구축 실습 spring.io 가이드를 참조하여 WebFlux 실습을 해보겠습니다. 개념은 이전 포스팅에서 대충 끄적인 걸로 대체하겠습니다. 목표 Spring Webflux 및 해당 서비스의 WebClient consumer로 RESTful 웹 서비스를 구축 환경 Spring Boot 2.4.2 (spring 5 이상) Maven IntelliJ openjdk 11 프로젝트 생성 우선 WebFlux 프로젝트를 생성합니다. 최초 생성 시의 pom.xml 내용입니다. 4.0.0 org.springframework.boot spring-boot-starter-parent 2.4.2 com.moonsiri reactive 0.0.1-SNAPSHOT reactive Reactive RESTful Web Service 11 o.. 2021. 2. 18.
[Spring] Reactive Programming (1) - Reactor 3, WebFlux Spring WebFlux The original web framework included in the Spring Framework, Spring Web MVC, was purpose-built for the Servlet API and Servlet containers. The reactive-stack web framework, Spring WebFlux, was added later in version 5.0. It is fully non-blocking, supports Reactive Streams back pressure, and runs on such servers as Netty, Undertow, and Servlet 3.1+ containers. 5.0 버전에 추가된 스프링 웹 플럭스.. 2021. 2. 18.
728x90
반응형