How to get response in Spring webclient

Viewed 40

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

0 Answers
Related