-
Couldn't load subscription status.
- Fork 38.8k
Description
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! π