Skip to content

Auto-configure Logbook interceptor for Spring RestClient - #2368

Open
kalayciburak wants to merge 2 commits into
zalando:mainfrom
kalayciburak:feat/restclient-logbook-customizer
Open

Auto-configure Logbook interceptor for Spring RestClient#2368
kalayciburak wants to merge 2 commits into
zalando:mainfrom
kalayciburak:feat/restclient-logbook-customizer

Conversation

@kalayciburak

Copy link
Copy Markdown

Description

Register a RestClientCustomizer that applies LogbookClientHttpRequestInterceptor to Spring RestClient.Builder instances.

This mirrors the existing auto-configuration support for Apache HttpClient 4/5 and Feign, so outbound RestClient traffic is logged automatically when Logbook's Spring Boot starter is on the classpath.

Motivation and Context

Fixes #2355

RestClient is the preferred outbound HTTP client in Spring Boot 4.x (Logbook 4.x target). Without this customizer, outbound calls are silent unless developers wire the interceptor manually.

Types of changes

  • New feature (non-breaking change which adds functionality)

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All commits are signed

Test plan

  • mvn -pl logbook-spring-boot-autoconfigure test -Dtest=RestClientTest — 3/3 GREEN
  • mvn -pl logbook-spring-boot-autoconfigure test — 101/101 GREEN (incl. JaCoCo)

Register a RestClientCustomizer that applies LogbookClientHttpRequestInterceptor
to RestClient.Builder instances, matching existing HttpClient and Feign
auto-configuration support.

Fixes zalando#2355
RestClient.class,
RestClientCustomizer.class
})
@ConditionalOnBean(LogbookClientHttpRequestInterceptor.class)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested the PR and the customizer is not getting configured because of a non match of this condition. It is because the LogbookClientHttpRequestInterceptor bean is configured in the same class sibling to the logbookRestClientCustomizer. Therefore it is not guaranteed that the interceptor bean is registered before the customizer.
Removing this condition solves the problem. And the condition on the RestClient class already implicitly ensures that the ClientHttpRequestInterceptor is also present, so the LogbookClientHttpRequestInterceptor applies.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, dropped the sibling condition. RestClient class check is enough

@ChristianLohmann

Copy link
Copy Markdown
Contributor

Hi @kalayciburak I'd like to add another test:

package org.zalando.logbook.autoconfigure;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.restclient.RestClientCustomizer;
import org.zalando.logbook.spring.LogbookClientHttpRequestInterceptor;

import static org.assertj.core.api.Assertions.assertThat;

@LogbookTest
class RestClientAutoConfigurationTest {

    @Autowired
    @Qualifier("logbookRestClientCustomizer")
    private RestClientCustomizer customizer;

    @Autowired
    private LogbookClientHttpRequestInterceptor interceptor;

    @Test
    void shouldAutoConfigureRestClientCustomizer() {
        assertThat(customizer).isNotNull();
        assertThat(interceptor).isNotNull();
    }

}

The interceptor is registered in the same auto-config class, so the
condition is not guaranteed. RestClient on the classpath already
implies the interceptor type.

Add RestClientAutoConfigurationTest for the full context.
@kalayciburak

Copy link
Copy Markdown
Author

added RestClientAutoConfigurationTest

@ChristianLohmann

Copy link
Copy Markdown
Contributor

👍

@kasmarian kasmarian added the minor Minor changes label Aug 19, 2026
@kasmarian

Copy link
Copy Markdown
Collaborator

@kalayciburak could you make sure that all commits in the PR are signed please?

@prashantpiyush1111 prashantpiyush1111 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The RestClient auto-configuration looks correct, and the added tests cover interceptor registration, application to RestClient.Builder, back-off behavior, and full application-context configuration. I did not find any code-level issues. The build is passing; only minor documentation/CI maintenance improvements remain.

@kasmarian

Copy link
Copy Markdown
Collaborator

@kalayciburak we can't merge the PR if all commits are not signed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

minor Minor changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Auto-configure LogbookClientHttpRequestInterceptor for Spring RestClient via RestClientCustomizer

4 participants