Run two APIs in parallel and get the first value with Spring Reactive
04:34 27 Aug 2026

I have two API calls. One of them is part of the legacy code of the project, let's call it OldAccountService and the new one is a simple WebClient calling a GET endpoint, let's call it NewAccountService.

The old service is not using reactive programming, hence is not using WebClient. The new one returns directly a Mono.

Here is the code of NewAccountService (simplified for example purposes)

public Mono retrieveAccountList(String id) {
return accountWebClient.get()
                .uri("/rest/AccountApi/id/{id}", id)
                .accept(MediaType.APPLICATION_JSON)
                .retrieve()
                .onStatus(HttpStatusCode::isError, response -> response.bodyToMono(String.class)
                        .flatMap(body -> Mono.error(
                                new RestClientException("AccountAPI error: status=" + response.statusCode() + ", body=" + body))))
                .bodyToMono(AccountListResponse.class)
                .map(accountListResponse -> AccountListResponseMapper.mapToAccount(
                        accountListResponse);
}

I need to call these two APIs in parallel and get the value from the first one that responds. This is what I tried so far:

Mono oldResult =
                    Mono.fromCallable(() -> oldAccountService.retrieveAccounts(...))
                            .subscribeOn(Schedulers.boundedElastic())
                            .map(oldAccount-> mapper.mapFrom(oldAccount))
                            .filter(AccountGateway::isValidObject)
                            .onErrorResume(ex -> Mono.empty());

Mono newResult =
                   newAccountService.retrieveAccountList(...)
                            .filter(AccountGateway::isValidObject)
                            .onErrorResume(ex -> Mono.empty());

return Mono.firstWithValue(oldResult , newResult )
                    .doOnNext(result -> log.info("One of the services responded sucessfully")
                    .onErrorMap(NoSuchElementException.class,
                            ex -> new AccountServiceException("No account data available from any source."));

Considering that I'm new with reactive programming I want to ask you for advice. Is this how you would do it? What can I improve or what is fundamentally wrong? Help me understand how Mono.fromCallable differs from what WebClient returns and if this is trully reactive? And last but not least, what would be the best way to handle errors?

java spring-webflux project-reactor reactive