Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot 2 - Transforming a Mono to a rx.Observable?

I'm trying to use the HystrixObservableCommand with the Spring WebFlux WebClient and I wonder if there is a "clean" to transform a Mono to an rx.Observable. My initial code looks like this:

public Observable<Comment> getComment() {

    return webClient.get()
            .uri(url)
            .accept(MediaType.APPLICATION_JSON)
            .retrieve()
            .bodyToMono(Comment.class)
            // stuff missing here :(.
}

Is there an easy to do this ?

Regards

like image 651
François-David Lessard Avatar asked Sep 16 '25 10:09

François-David Lessard


1 Answers

The recommended approach is to use RxJavaReactiveStreams, more specifically:

public Observable<Comment> getComment() {

    Mono<Comment> mono = webClient.get()
            .uri(url)
            .accept(MediaType.APPLICATION_JSON)
            .retrieve()
            .bodyToMono(Comment.class);

    return RxReactiveStreams.toObservable(mono); // <-- convert any Publisher to RxJava 1
}
like image 114
Simon Baslé Avatar answered Sep 19 '25 04:09

Simon Baslé