Skip to content

Enable RestClientAutoConfiguration in reactive web applications when virtual threads are enabled #44952

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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 {

}

}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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 {