Skip to content

Commit

Permalink
Do not set ClientHttpRequestFactory when Spring Boot >= 3.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielLiu1123 committed Nov 22, 2024
1 parent 33720da commit 79ca053
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
7 changes: 7 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,16 @@ allprojects {
}

test {
systemProperties("spring.cloud.compatibility-verifier.enabled": "false")
useJUnitPlatform()
}

tasks.configureEach {
doFirst {
System.setProperty("spring.cloud.compatibility-verifier.enabled", "false")
}
}

apply plugin: "com.diffplug.spotless"
spotless {
encoding "UTF-8"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.boot.SpringBootVersion;
import org.springframework.boot.autoconfigure.web.client.RestClientBuilderConfigurer;
import org.springframework.boot.autoconfigure.web.client.RestTemplateBuilderConfigurer;
import org.springframework.boot.ssl.SslBundle;
Expand Down Expand Up @@ -234,7 +235,10 @@ private RestTemplate buildRestTemplate(HttpExchangeProperties.Channel channelCon
}

// Set default request factory
builder = builder.requestFactory(() -> getRequestFactory(channelConfig));
// No need to do this when Spring Boot version >= 3.4.0
if (isSpringBootVersionLessThan340()) {
builder = builder.requestFactory(() -> getRequestFactory(channelConfig));
}

if (isLoadBalancerEnabled(channelConfig)) {
Set<ClientHttpRequestInterceptor> lbInterceptors = new LinkedHashSet<>();
Expand Down Expand Up @@ -322,12 +326,14 @@ private RestClient buildRestClient(HttpExchangeProperties.Channel channelConfig)
header.getKey(), header.getValues().toArray(String[]::new)));
}

ClientHttpRequestFactory requestFactory =
unwrapRequestFactoryIfNecessary(getFieldValue(builder, "requestFactory"));
if (requestFactory == null) {
builder.requestFactory(getRequestFactory(channelConfig));
} else {
setTimeoutByConfig(requestFactory, channelConfig);
if (isSpringBootVersionLessThan340()) {
ClientHttpRequestFactory requestFactory =
unwrapRequestFactoryIfNecessary(getFieldValue(builder, "requestFactory"));
if (requestFactory == null) {
builder.requestFactory(getRequestFactory(channelConfig));
} else {
setTimeoutByConfig(requestFactory, channelConfig);
}
}

if (isLoadBalancerEnabled(channelConfig)) {
Expand Down Expand Up @@ -357,6 +363,10 @@ private RestClient buildRestClient(HttpExchangeProperties.Channel channelConfig)
return builder.build();
}

private static boolean isSpringBootVersionLessThan340() {
return SpringBootVersion.getVersion().compareTo("3.4.0") < 0;
}

private ClientHttpRequestFactory getRequestFactory(HttpExchangeProperties.Channel channelConfig) {
ClientHttpRequestFactorySettings settings = new ClientHttpRequestFactorySettings(
Optional.ofNullable(channelConfig.getConnectTimeout())
Expand Down

0 comments on commit 79ca053

Please sign in to comment.