diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/client/NotReactiveWebApplicationOrVirtualThreadsEnabledCondition.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/client/NotReactiveWebApplicationOrVirtualThreadsEnabledCondition.java new file mode 100644 index 000000000000..9e87fffbca4f --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/client/NotReactiveWebApplicationOrVirtualThreadsEnabledCondition.java @@ -0,0 +1,47 @@ +/* + * Copyright 2012-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.web.client; + +import org.springframework.boot.autoconfigure.condition.AnyNestedCondition; +import org.springframework.boot.autoconfigure.condition.ConditionalOnThreading; +import org.springframework.boot.autoconfigure.condition.SpringBootCondition; +import org.springframework.boot.autoconfigure.thread.Threading; +import org.springframework.context.annotation.Conditional; + +/** + * {@link SpringBootCondition} that applies when running in a non-reactive web application + * or virtual threads are enabled. + * + * @author Dmitry Sulman + */ +class NotReactiveWebApplicationOrVirtualThreadsEnabledCondition extends AnyNestedCondition { + + NotReactiveWebApplicationOrVirtualThreadsEnabledCondition() { + super(ConfigurationPhase.PARSE_CONFIGURATION); + } + + @Conditional(NotReactiveWebApplicationCondition.class) + private static final class NotReactiveWebApplication { + + } + + @ConditionalOnThreading(Threading.VIRTUAL) + private static final class VirtualThreadsEnabled { + + } + +} diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/client/RestClientAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/client/RestClientAutoConfiguration.java index f89fc31b6609..077217a1ddb8 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/client/RestClientAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/client/RestClientAutoConfiguration.java @@ -53,7 +53,7 @@ @AutoConfiguration(after = { HttpClientAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class, SslAutoConfiguration.class }) @ConditionalOnClass(RestClient.class) -@Conditional(NotReactiveWebApplicationCondition.class) +@Conditional(NotReactiveWebApplicationOrVirtualThreadsEnabledCondition.class) public class RestClientAutoConfiguration { @Bean diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/client/RestClientAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/client/RestClientAutoConfigurationTests.java index fae799d96a97..89d8c42b145a 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/client/RestClientAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/client/RestClientAutoConfigurationTests.java @@ -19,6 +19,8 @@ import java.util.List; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledForJreRange; +import org.junit.jupiter.api.condition.JRE; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.http.HttpMessageConverters; @@ -29,6 +31,8 @@ import org.springframework.boot.http.client.ClientHttpRequestFactorySettings.Redirects; import org.springframework.boot.ssl.SslBundles; import org.springframework.boot.test.context.runner.ApplicationContextRunner; +import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner; +import org.springframework.boot.test.context.runner.WebApplicationContextRunner; import org.springframework.boot.web.client.RestClientCustomizer; import org.springframework.boot.web.codec.CodecCustomizer; import org.springframework.context.annotation.Bean; @@ -225,6 +229,39 @@ void shouldSupplyRestClientBuilderConfigurerWithAutoConfiguredHttpSettings() { }); } + @Test + void whenReactiveWebApplicationRestClientIsNotConfigured() { + new ReactiveWebApplicationContextRunner() + .withConfiguration(AutoConfigurations.of(RestClientAutoConfiguration.class)) + .run((context) -> { + assertThat(context).doesNotHaveBean(HttpMessageConvertersRestClientCustomizer.class); + assertThat(context).doesNotHaveBean(RestClientBuilderConfigurer.class); + assertThat(context).doesNotHaveBean(RestClient.Builder.class); + }); + } + + @Test + void whenServletWebApplicationRestClientIsConfigured() { + new WebApplicationContextRunner().withConfiguration(AutoConfigurations.of(RestClientAutoConfiguration.class)) + .run((context) -> { + assertThat(context).hasSingleBean(HttpMessageConvertersRestClientCustomizer.class); + assertThat(context).hasSingleBean(RestClientBuilderConfigurer.class); + assertThat(context).hasSingleBean(RestClient.Builder.class); + }); + } + + @Test + @EnabledForJreRange(min = JRE.JAVA_21) + void whenReactiveWebApplicationAndVirtualThreadsAreEnabledOnJava21AndLaterRestClientIsConfigured() { + new ReactiveWebApplicationContextRunner().withPropertyValues("spring.threads.virtual.enabled=true") + .withConfiguration(AutoConfigurations.of(RestClientAutoConfiguration.class)) + .run((context) -> { + assertThat(context).hasSingleBean(HttpMessageConvertersRestClientCustomizer.class); + assertThat(context).hasSingleBean(RestClientBuilderConfigurer.class); + assertThat(context).hasSingleBean(RestClient.Builder.class); + }); + } + @Configuration(proxyBeanMethods = false) static class CodecConfiguration {