How to get response in Spring webclient

Issue

This Content is from Stack Overflow. Question asked by cppcoder

I have the following method which calls a rest api using webclient in a library.

public Flux<Offer[]> getOffersByIds(List<String> ids) {
    return Flux.fromStream(batch(ids, 100)).flatMap(batch -> getOffersForBatch(batch));
}

private Mono<Offer[]> getOffersForBatch(List<String> ids) {
    return webClient.get().uri(URL + PROMOTIONS_ENDPOINT, uriBuilder -> uriBuilder
                    .queryParam("ids", String.join(",", ids))
                    .build())
            .header("Accept-Language", "en-GB")
            .retrieve()
            .onStatus(HttpStatus::is5xxServerError, response -> Mono.error(new RetryableException("api error")))
            .bodyToMono(Offer[].class)
            .retryWhen(Retry.backoff(3, Duration.ofSeconds(5))
                    .jitter(0.75)
                    .filter(throwable -> throwable instanceof RunException)
                    .onRetryExhaustedThrow(((retryBackoffSpec, retrySignal) -> {
                        throw new ServerException("service failed after max retries");
                    })));
}

public static <T> Stream<List<T>> batch(List<T> source, int length) {
    if (length <= 0)
        throw new IllegalArgumentException("length = " + length);
    int size = source.size();
    if (size <= 0)
        return Stream.empty();
    int fullChunks = (size - 1) / length;
    return IntStream.range(0, fullChunks + 1).mapToObj(
            n -> source.subList(n * length, n == fullChunks ? size : (n + 1) * length));
}

In the service, I am trying to invoke the api:

offService.getOffersByIds(ids)
          .subscribe(
                  success -> log.info("Success:" + success.toString()),
                  error -> log.error("Failure:" + error.getMessage()),
                  () -> log.error("No value")
          );

But I don’t see any logs related to response



Solution

This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?