Provide RestClientSsl as a bean
Closes gh-37400
This commit is contained in:
@@ -19,10 +19,13 @@ package org.springframework.boot.autoconfigure.web.client;
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
|
||||
import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.ssl.SslAutoConfiguration;
|
||||
import org.springframework.boot.ssl.SslBundles;
|
||||
import org.springframework.boot.web.client.ClientHttpRequestFactories;
|
||||
import org.springframework.boot.web.client.ClientHttpRequestFactorySettings;
|
||||
import org.springframework.boot.web.client.RestClientCustomizer;
|
||||
@@ -44,7 +47,7 @@ import org.springframework.web.client.RestClient;
|
||||
* @author Moritz Halbritter
|
||||
* @since 3.2.0
|
||||
*/
|
||||
@AutoConfiguration(after = HttpMessageConvertersAutoConfiguration.class)
|
||||
@AutoConfiguration(after = { HttpMessageConvertersAutoConfiguration.class, SslAutoConfiguration.class })
|
||||
@ConditionalOnClass(RestClient.class)
|
||||
@Conditional(NotReactiveWebApplicationCondition.class)
|
||||
public class RestClientAutoConfiguration {
|
||||
@@ -57,6 +60,13 @@ public class RestClientAutoConfiguration {
|
||||
return new HttpMessageConvertersRestClientCustomizer(messageConverters.getIfUnique());
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(RestClientSsl.class)
|
||||
@ConditionalOnBean(SslBundles.class)
|
||||
AutoConfiguredRestClientSsl restClientSsl(SslBundles sslBundles) {
|
||||
return new AutoConfiguredRestClientSsl(sslBundles);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
RestClientBuilderConfigurer restClientBuilderConfigurer(ObjectProvider<RestClientCustomizer> customizerProvider) {
|
||||
|
||||
@@ -32,7 +32,7 @@ import org.springframework.web.client.RestClient;
|
||||
* Typically used as follows: <pre class="code">
|
||||
* @Bean
|
||||
* public MyBean myBean(RestClient.Builder restClientBuilder, RestClientSsl ssl) {
|
||||
* RestClient restClientrestClient= restClientBuilder.apply(ssl.forBundle("mybundle")).build();
|
||||
* RestClient restClientrestClient= restClientBuilder.apply(ssl.fromBundle("mybundle")).build();
|
||||
* return new MyBean(webClient);
|
||||
* }
|
||||
* </pre> NOTE: Apply SSL configuration will replace any previously
|
||||
|
||||
@@ -23,6 +23,7 @@ import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
|
||||
import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration;
|
||||
import org.springframework.boot.ssl.SslBundles;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.boot.web.client.RestClientCustomizer;
|
||||
import org.springframework.boot.web.codec.CodecCustomizer;
|
||||
@@ -59,6 +60,12 @@ class RestClientAutoConfigurationTests {
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldSupplyRestClientSslIfSslBundlesIsThere() {
|
||||
this.contextRunner.withBean(SslBundles.class, () -> mock(SslBundles.class))
|
||||
.run((context) -> assertThat(context).hasSingleBean(RestClientSsl.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldCreateBuilder() {
|
||||
this.contextRunner.run((context) -> {
|
||||
|
||||
Reference in New Issue
Block a user