Skip to content

RestClient.builder() prefers a global HttpComponentsClientHttpRequestFactoryBuilder Spring Bean instead of instance supplied in .requestFactory() parameterΒ #34384

@user1bh

Description

@user1bh

Imagine the following code which creates a custom RestClient instance, specifically using a custom requestFactory, no beans:

val restClient = RestClient
    .builder()
    .requestFactory(ClientHttpRequestFactoryBuilder.httpComponents().build())
    .baseUrl(config.baseUrl)
    .defaultHeader(USER_AGENT_HEADER_NAME, USER_AGENT_HEADER_VALUE)
    .defaultUriVariables(mapOf(CLIENT_ID_PARAMETER_NAME to config.clientId))
    .requestInterceptor(BasicAuthenticationInterceptor(config.clientId, config.clientSecret))
    .apply(restClientSsl.fromBundle(REST_CLIENT_SSL_BUNDLE_NAME))
    .build()

When this RestClient is created - I would expect it to use a custom request factory based on standard ClientHttpRequestFactoryBuilder.httpComponents().build(), no Spring Beans involved.

However, instead, this RestClient instance still uses globally defined Spring Bean, for example:

@Bean
fun httpComponentsClientHttpRequestFactoryBuilder(): HttpComponentsClientHttpRequestFactoryBuilder {
    return ClientHttpRequestFactoryBuilder.httpComponents().withDefaultRequestConfigCustomizer() {
        it.setProtocolUpgradeEnabled(false)
    }
}

It means that I can't create a custom instance of RestClient that ignores global configuration and instead I have to globally override the Spring Bean instance of HttpComponentsClientHttpRequestFactoryBuilder

Am I doing something wrong? :)
I'm not that good with Spring however, so thanks for pointing out to the right Spring configuration, etc.
There is some info here but I'm not sure.

Using Spring Boot org.springframework.boot version 3.4.2, spring-web 6.2.2.

Thanks! πŸ˜„

Metadata

Metadata

Assignees

No one assigned

    Labels

    for: stackoverflowA question that's better suited to stackoverflow.comin: webIssues in web modules (web, webmvc, webflux, websocket)status: invalidAn issue that we don't feel is valid

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions