Search code examples
springspring-mvcspring-bootrx-javahystrix

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


Solution

  • 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
    }