Spring Top

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE

1. Overview

In this tutorial, we are going to show how to customize Spring's WebClient – a reactive HTTP client – to log requests and responses.

2. WebClient

WebClient is a reactive and non-blocking interface for HTTP requests, based on Spring WebFlux. It has a functional, fluent API with reactive types for declarative composition.

Behind the scenes, WebClient calls an HTTP client. Reactor Netty is the default and reactive HttpClient of Jetty is also supported. Moreover, it's possible to plug other implementations of HTTP client by setting up a ClientConnector for WebClient.

3. Logging Requests and Responses

The default HttpClient used by WebClient is the Netty implementation, so after we change the reactor.netty.http.client logging level to DEBUG, we can see some request logging, but if we need a customized log, we can configure our loggers via WebClient#filters:

WebClient
  .builder()
  .filters(exchangeFilterFunctions -> {
      exchangeFilterFunctions.add(logRequest());
      exchangeFilterFunctions.add(logResponse());
  })
  .build()

In this code snippet, we've added two separate filters to log the request and the response.

Let's implement logRequest by using ExchangeFilterFunction#ofRequestProcessor:

ExchangeFilterFunction logRequest() {
    return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {
        if (log.isDebugEnabled()) {
            StringBuilder sb = new StringBuilder("Request: \n");
            //append clientRequest method and url
            clientRequest
              .headers()
              .forEach((name, values) -> values.forEach(value -> /* append header key/value */));
            log.debug(sb.toString());
        }
        return Mono.just(clientRequest);
    });
}

logResponse is the same, but we have to use ExchangeFilterFunction#ofResponseProcessor instead.

Now we can change the reactor.netty.http.client log level to INFO or ERROR to have a cleaner output.

4. Logging Request and Response with Body

HTTP clients have features to log the bodies of requests and responses. Thus, to achieve the goal, we are going to use a log-enabled HTTP client with our WebClient.

We can do this by manually setting WebClient.Builder#clientConnector – let's see with Jetty and Netty HTTP clients.

4.1. Logging with Jetty HttpClient

First, let's add the Maven dependency for jetty-reactive-httpclient to our pom:

<dependency>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-reactive-httpclient</artifactId>
    <version>1.0.3</version>
</dependency>

Then we're going to create a customized Jetty HttpClient:

SslContextFactory.Client sslContextFactory = new SslContextFactory.Client();
HttpClient httpClient = new HttpClient(sslContextFactory) {
    @Override
    public Request newRequest(URI uri) {
        Request request = super.newRequest(uri);
        return enhance(request);
    }
};

Here, we've overridden HttpClient#newRequest, then wrapped the Request in a log enhancer.

Next, we need to register events with the request so that we can log as each part of the request becomes available:

Request enhance(Request request) {
    StringBuilder group = new StringBuilder();
    request.onRequestBegin(theRequest -> {
        // append request url and method to group
    });
    request.onRequestHeaders(theRequest -> {
        for (HttpField header : theRequest.getHeaders()) {
            // append request headers to group
        }
    });
    request.onRequestContent((theRequest, content) -> {
        // append content to group
    });
    request.onRequestSuccess(theRequest -> {
        log.debug(group.toString());
        group.delete(0, group.length());
    });
    group.append("\n");
    request.onResponseBegin(theResponse -> {
        // append response status to group
    });
    request.onResponseHeaders(theResponse -> {
        for (HttpField header : theResponse.getHeaders()) {
            // append response headers to group
        }
    });
    request.onResponseContent((theResponse, content) -> {
        // append content to group
    });
    request.onResponseSuccess(theResponse -> {
        log.debug(group.toString());
    });
    return request;
}

Finally, we have to build the WebClient instance:

WebClient
  .builder()
  .clientConnector(new JettyClientHttpConnector(httpClient))
  .build()

Of course, as we did before, we'll need to set the log level of RequestLogEnhancer to DEBUG.

4.2. Logging with Netty HttpClient


First, let's create a Netty HttpClient:

HttpClient httpClient = HttpClient
  .create()
  .wiretap(true)

Having enabled the wiretap, each request and response will be logged in full detail.

Next, we have to set the log level of Netty's client package reactor.netty.http.client to DEBUG:

logging.level.reactor.netty.http.client=DEBUG

Now, let's build the WebClient:

WebClient
  .builder()
  .clientConnector(new ReactorClientHttpConnector(httpClient))
  .build()

Our WebClient will log every request and response in full detail, but the default format of Netty built-in logger contains both Hex and Text representation of bodies and also a lot of data about request and response events.

So, if we need a customized logger for Netty, we can configure the HttpClient:

HttpClient httpClient = HttpClient
  .create()
  .tcpConfiguration(
    tc -> tc.bootstrap(
      b -> BootstrapHandlers.updateLogSupport(b, new CustomLogger(HttpClient.class))))
  .build()

Last, let's implement our CustomLogger that extends LoggingHandler:

public class CustomLogger extends LoggingHandler {
    public CustomLogger(Class<?> clazz) {
        super(clazz);
    }

    @Override
    protected String format(ChannelHandlerContext ctx, String event, Object arg) {
        if (arg instanceof ByteBuf) {
            ByteBuf msg = (ByteBuf) arg;
            return decode(
              msg, msg.readerIndex(), msg.readableBytes(), defaultCharset());
        }
        return super.format(ctx, event, arg);
    }
    
    // further code omitted for brevity
}

5. Conclusion

In this tutorial, we've used several techniques for logging request and response data while using Spring WebClient.

As always the code is available over on GitHub.

Spring bottom

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE
newest oldest most voted
Notify of
dobrin
Guest
dobrin

Thanks, I was searching for how to dump requests and responses with WebClient. I just saw that it splits the body into multiple lines logged by reactor.netty.http.client.HttpClient. So is this done in order to omit some buffering / for efficiency or?

Also is there a way to join all the body logs to get it nice into the log?

Eric Martin
Member
Eric Martin

Please take a look at the implementation of decode() here: https://github.com/eugenp/tutorials/blob/master/spring-5-reactive-client/src/test/java/com/baeldung/reactive/logging/netty/CustomLogger.java Also, here is the documentation for ByteBuf: https://netty.io/4.0/api/io/netty/buffer/ByteBuf.html After reviewing both of these, you can see that Netty provides the body in chunks. And yes, it’s because of efficiency. If the body is small enough, it provides the body as a whole. It’s possible to increase the buffer size, for example like this: https://stackoverflow.com/questions/26662990/bytebuf-initial-capacity-size But it probably won’t solve your problem, since if you want every possible body to fit, the performance will be significantly less, especially for smaller bodies. In theory, you could collect the whole body and… Read more »

Comments are closed on this article!