'WebClient should ignore unsucessful requests

The following code causes an empty list if url1 returns many results but url2 returns a 404. How to ignore error properly and just continue with the succesful responses of url1?

        Set<String> list = new HashSet<>();

    Mono<String> first = WebClient.create(url1)
            .get()
            .retrieve()
            .bodyToMono(String.class)
            .onErrorResume(e -> {
                return Mono.empty();
            });

    Mono<String> two = WebClient.create()
            .get()
            .retrieve()
            .bodyToMono(String.class)
            .onErrorResume(e -> {
                return Mono.empty();
            });

    Flux.zip(first, two)
            .doOnComplete(() -> System.out.printf("done: %d\n", list.size()))
            .subscribe(
                    responses -> {
                        for (Object response : responses) {
                            String[] lines = ((String)response).split("\n");
                            for(String line : lines) {
                                list.add(line);
                            }
                        }
                    }
            );


Solution 1:[1]

I've simplified your example, replacing the two webclient calls with their expected output, mainly for demonstration purposes.

You can use merge instead zip, to avoid the entire Flux returning empty if one single source is empty.

Flux.merge(Mono.just("1\n2\n3"), Mono.empty())
                .doOnComplete(() -> System.out.printf("done: %d\n", list.size()))
                .subscribe(
                        responses -> {
                            List<String> stringList = Arrays.stream(responses.split("\n")).collect(Collectors.toList());
                            stringList.forEach(System.out::println);
                        });

JavaDoc

NOTE

It's worth mentioning that processing the result in the subscribe method and adding it to a variable defined outside of the reactive stream is a little unusual. The more expected use case would be to process the data in a flatMap/map and return either a Mono<List<String>> or Flux<String> out of your method ie.

Example returning a Mono<List<String>>

Flux.merge(Mono.just("1\n2\n3"), Mono.empty())
                .doOnComplete(() -> System.out.printf("done: %d\n", list.size()))
                .flatMap(strings -> Flux.fromArray(strings.split("\n")))
                .collectList()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1