Skip to content

Commit 740b00e

Browse files
committed
Add condition for ClientHttpConnectorBuilder detection
Update `ClientHttpConnectorAutoConfiguration` with a condition to ensure that `ClientHttpConnectorBuilder.detect` will return a result. Prior to this commit, when using a JDK without `java.net.http.HttpClient` access the auto-configuration would fail. Fixes gh-45955
1 parent 7613594 commit 740b00e

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/http/client/reactive/ClientHttpConnectorAutoConfiguration.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.springframework.boot.ssl.SslBundles;
3535
import org.springframework.boot.util.LambdaSafe;
3636
import org.springframework.context.annotation.Bean;
37+
import org.springframework.context.annotation.Conditional;
3738
import org.springframework.context.annotation.Configuration;
3839
import org.springframework.context.annotation.Import;
3940
import org.springframework.context.annotation.Lazy;
@@ -48,6 +49,7 @@
4849
*/
4950
@AutoConfiguration(after = SslAutoConfiguration.class)
5051
@ConditionalOnClass({ ClientHttpConnector.class, Mono.class })
52+
@Conditional(ConditionalOnClientHttpConnectorBuilderDetection.class)
5153
@EnableConfigurationProperties(HttpReactiveClientProperties.class)
5254
public class ClientHttpConnectorAutoConfiguration implements BeanClassLoaderAware {
5355

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2012-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.http.client.reactive;
18+
19+
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
20+
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
21+
import org.springframework.boot.http.client.reactive.ClientHttpConnectorBuilder;
22+
import org.springframework.context.annotation.Condition;
23+
import org.springframework.context.annotation.ConditionContext;
24+
import org.springframework.core.type.AnnotatedTypeMetadata;
25+
26+
/**
27+
* {@link Condition} that checks that {@link ClientHttpConnectorBuilder} can be detected.
28+
*
29+
* @author Phillip Webb
30+
*/
31+
class ConditionalOnClientHttpConnectorBuilderDetection extends SpringBootCondition {
32+
33+
@Override
34+
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
35+
try {
36+
ClientHttpConnectorBuilder.detect(context.getClassLoader());
37+
return ConditionOutcome.match("Detected ClientHttpConnectorBuilder");
38+
}
39+
catch (IllegalStateException ex) {
40+
return ConditionOutcome.noMatch("Unable to detect ClientHttpConnectorBuilder");
41+
}
42+
}
43+
44+
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/http/client/reactive/ClientHttpConnectorAutoConfigurationTests.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.springframework.http.client.reactive.ClientHttpConnector;
4242

4343
import static org.assertj.core.api.Assertions.assertThat;
44+
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
4445
import static org.mockito.Mockito.mock;
4546

4647
/**
@@ -143,6 +144,17 @@ void configuresClientHttpConnectorSettings() {
143144
});
144145
}
145146

147+
@Test
148+
void shouldBeConditionalOnAtLeastOneHttpConnectorClass() {
149+
FilteredClassLoader classLoader = new FilteredClassLoader(reactor.netty.http.client.HttpClient.class,
150+
org.eclipse.jetty.client.HttpClient.class, org.apache.hc.client5.http.impl.async.HttpAsyncClients.class,
151+
java.net.http.HttpClient.class);
152+
assertThatIllegalStateException().as("enough filtering")
153+
.isThrownBy(() -> ClientHttpConnectorBuilder.detect(classLoader));
154+
this.contextRunner.withClassLoader(classLoader)
155+
.run((context) -> assertThat(context).doesNotHaveBean(ClientHttpConnectorSettings.class));
156+
}
157+
146158
private List<String> sslPropertyValues() {
147159
List<String> propertyValues = new ArrayList<>();
148160
String location = "classpath:org/springframework/boot/autoconfigure/ssl/";

0 commit comments

Comments
 (0)