Accessing the response body with Spring WebFlux WebClient with onStatus

Featured image for sharing metadata for article

While writing some integration tests for a WebClient today, I wanted to take advantage of the onStatus method to perform some operational logging using the response body, before I mapped it to a particular exception.

I found it really quite difficult to do, as I wanted to use .block(), but found it didn't work, and other means I'd tried just ended up not executing.

I found that the following ended up working:

import org.junit.jupiter.api.Test;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.reactive.function.client.WebClient;

class Example {
  @Test
  void example() {
    WebClient webClient = WebClient.builder().build();

    webClient
        .get()
        .uri("http://localhost:4010/")
        .retrieve()
        .onStatus(
            s -> s.equals(HttpStatus.NOT_FOUND),
            (e) ->
                e.bodyToMono(ErrorResponse.class)
                    .handle(
                        (body, handler) -> {
                          LoggerFactory.getLogger(getClass()).warn("HTTP 404 returned: {}", body);
                          handler.error(new BusinessLogicException());
                        }))
        .toBodilessEntity()
        .block();
  }

  private static class BusinessLogicException extends Exception {}

  private record ErrorResponse(String type, String title, long status, String detail) {}
}

This use of handle allows it to process asynchronously, giving us the chance to interact with the parsed response body.

Written by Jamie Tanna's profile image Jamie Tanna on , and last updated on .

Content for this article is shared under the terms of the Creative Commons Attribution Non Commercial Share Alike 4.0 International, and code is shared under the Apache License 2.0.

#blogumentation #spring #spring-boot #java.

Also on:

This post was filed under articles.

Interactions with this post

Interactions with this post

Below you can find the interactions that this page has had using WebMention.

Have you written a response to this post? Let me know the URL:

Do you not have a website set up with WebMention capabilities? You can use Comment Parade.