Skip to content

Commit 4ce726b

Browse files
committed
Auto-configure WebClient.Builder
This commit adds a new customizer interface for applying configuration changes to `WebClient.Builder` beans: `WebClientCustomizer`. The new WebClient auto-configuration will make available, as a prototype scoped bean, `WebClient.Builder` instances. Once injected, developers can use those to create `WebClient` instances to be used in their application. `WebClientCustomizer` beans are sorted according to their `Order` and then applied to the builder instances. Closes gh-9522
1 parent 89af4a6 commit 4ce726b

File tree

6 files changed

+237
-4
lines changed

6 files changed

+237
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2012-2017 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+
* http://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.web.reactive.function.client;
18+
19+
import java.util.ArrayList;
20+
import java.util.List;
21+
22+
import org.springframework.beans.factory.ObjectProvider;
23+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
24+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
25+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
26+
import org.springframework.boot.web.reactive.function.client.WebClientCustomizer;
27+
import org.springframework.context.annotation.Bean;
28+
import org.springframework.context.annotation.Configuration;
29+
import org.springframework.context.annotation.Scope;
30+
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
31+
import org.springframework.util.CollectionUtils;
32+
import org.springframework.web.reactive.function.client.WebClient;
33+
34+
/**
35+
* {@link EnableAutoConfiguration Auto-configuration} for {@link WebClient}.
36+
* <p>This will produce a {@link WebClient.Builder} bean with the {@code prototype} scope,
37+
* meaning each injection point will receive a newly cloned instance of the builder.
38+
*
39+
* @author Brian Clozel
40+
* @since 2.0.0
41+
*/
42+
@Configuration
43+
@ConditionalOnClass(WebClient.class)
44+
public class WebClientAutoConfiguration {
45+
46+
private final WebClient.Builder webClientBuilder;
47+
48+
49+
public WebClientAutoConfiguration(ObjectProvider<List<WebClientCustomizer>> customizerProvider) {
50+
this.webClientBuilder = WebClient.builder();
51+
List<WebClientCustomizer> customizers = customizerProvider.getIfAvailable();
52+
if (!CollectionUtils.isEmpty(customizers)) {
53+
customizers = new ArrayList<>(customizers);
54+
AnnotationAwareOrderComparator.sort(customizers);
55+
customizers.forEach(customizer -> customizer.customize(this.webClientBuilder));
56+
}
57+
}
58+
59+
@Bean
60+
@Scope("prototype")
61+
@ConditionalOnMissingBean
62+
public WebClient.Builder webClientBuilder(List<WebClientCustomizer> customizers) {
63+
return this.webClientBuilder.clone();
64+
}
65+
}

Diff for: spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories

+1
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration,
114114
org.springframework.boot.autoconfigure.web.reactive.HttpHandlerAutoConfiguration,\
115115
org.springframework.boot.autoconfigure.web.reactive.ReactiveWebServerAutoConfiguration,\
116116
org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration,\
117+
org.springframework.boot.autoconfigure.web.reactive.function.client.WebClientAutoConfiguration,\
117118
org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration,\
118119
org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration,\
119120
org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration,\

Diff for: spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/WebFluxAutoConfigurationTests.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ public void shouldRegisterCustomHandlerMethodArgumentResolver() throws Exception
105105
.getBean(RequestMappingHandlerAdapter.class);
106106
assertThat((List<HandlerMethodArgumentResolver>) ReflectionTestUtils
107107
.getField(adapter.getArgumentResolverConfigurer(), "customResolvers"))
108-
.contains(
109-
this.context.getBean("firstResolver",
110-
HandlerMethodArgumentResolver.class),
108+
.contains(
109+
this.context.getBean("firstResolver",
110+
HandlerMethodArgumentResolver.class),
111111
this.context.getBean("secondResolver",
112112
HandlerMethodArgumentResolver.class));
113113
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
* Copyright 2012-2017 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+
* http://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.web.reactive.function.client;
18+
19+
import java.net.URI;
20+
21+
import org.junit.After;
22+
import org.junit.Test;
23+
import reactor.core.publisher.Mono;
24+
25+
import org.springframework.boot.web.reactive.function.client.WebClientCustomizer;
26+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
27+
import org.springframework.context.annotation.Bean;
28+
import org.springframework.context.annotation.Configuration;
29+
import org.springframework.http.HttpMethod;
30+
import org.springframework.http.client.reactive.ClientHttpConnector;
31+
import org.springframework.web.reactive.function.client.WebClient;
32+
33+
import static org.assertj.core.api.Assertions.assertThat;
34+
import static org.mockito.ArgumentMatchers.any;
35+
import static org.mockito.ArgumentMatchers.eq;
36+
import static org.mockito.BDDMockito.given;
37+
import static org.mockito.Mockito.mock;
38+
import static org.mockito.Mockito.times;
39+
import static org.mockito.Mockito.verify;
40+
41+
/**
42+
* Tests for {@link WebClientAutoConfiguration}
43+
*
44+
* @author Brian Clozel
45+
*/
46+
public class WebClientAutoConfigurationTests {
47+
48+
private AnnotationConfigApplicationContext context;
49+
50+
@After
51+
public void close() {
52+
if (this.context != null) {
53+
this.context.close();
54+
}
55+
}
56+
57+
@Test
58+
public void webClientShouldApplyCustomizers() throws Exception {
59+
load(WebClientCustomizerConfig.class);
60+
WebClient.Builder builder = this.context.getBean(WebClient.Builder.class);
61+
WebClientCustomizer customizer = this.context.getBean(WebClientCustomizer.class);
62+
builder.build();
63+
verify(customizer).customize(any(WebClient.Builder.class));
64+
}
65+
66+
@Test
67+
public void shouldGetPrototypeScopedBean() throws Exception {
68+
load(WebClientCustomizerConfig.class);
69+
70+
ClientHttpConnector firstConnector = mock(ClientHttpConnector.class);
71+
given(firstConnector.connect(any(), any(), any())).willReturn(Mono.empty());
72+
WebClient.Builder firstBuilder = this.context.getBean(WebClient.Builder.class);
73+
firstBuilder.clientConnector(firstConnector).baseUrl("http://first.example.org");
74+
75+
ClientHttpConnector secondConnector = mock(ClientHttpConnector.class);
76+
given(secondConnector.connect(any(), any(), any())).willReturn(Mono.empty());
77+
WebClient.Builder secondBuilder = this.context.getBean(WebClient.Builder.class);
78+
secondBuilder.clientConnector(secondConnector).baseUrl("http://second.example.org");
79+
80+
assertThat(firstBuilder).isNotEqualTo(secondBuilder);
81+
82+
firstBuilder.build().get().uri("/foo").exchange().block();
83+
secondBuilder.build().get().uri("/foo").exchange().block();
84+
85+
verify(firstConnector).connect(eq(HttpMethod.GET), eq(URI.create("http://first.example.org/foo")), any());
86+
verify(secondConnector).connect(eq(HttpMethod.GET), eq(URI.create("http://second.example.org/foo")), any());
87+
WebClientCustomizer customizer = this.context.getBean(WebClientCustomizer.class);
88+
verify(customizer, times(1)).customize(any(WebClient.Builder.class));
89+
}
90+
91+
@Test
92+
public void shouldNotCreateClientBuilderIfAlreadyPresent() throws Exception {
93+
load(WebClientCustomizerConfig.class, CustomWebClientBuilderConfig.class);
94+
WebClient.Builder builder = this.context.getBean(WebClient.Builder.class);
95+
assertThat(builder).isInstanceOf(MyWebClientBuilder.class);
96+
}
97+
98+
99+
private void load(Class<?>... config) {
100+
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
101+
ctx.register(config);
102+
ctx.register(WebClientAutoConfiguration.class);
103+
ctx.refresh();
104+
this.context = ctx;
105+
}
106+
107+
@Configuration
108+
static class WebClientCustomizerConfig {
109+
110+
@Bean
111+
public WebClientCustomizer webClientCustomizer() {
112+
return mock(WebClientCustomizer.class);
113+
}
114+
115+
}
116+
117+
@Configuration
118+
static class CustomWebClientBuilderConfig {
119+
120+
@Bean
121+
public MyWebClientBuilder myWebClientBuilder() {
122+
return mock(MyWebClientBuilder.class);
123+
}
124+
125+
}
126+
127+
interface MyWebClientBuilder extends WebClient.Builder {
128+
129+
}
130+
131+
}

Diff for: spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories

+2-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ org.springframework.boot.test.autoconfigure.web.client.WebClientRestTemplateAuto
110110
org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration,\
111111
org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration,\
112112
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration,\
113-
org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration
113+
org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration,\
114+
org.springframework.boot.autoconfigure.web.reactive.function.client.WebClientAutoConfiguration
114115

115116
# AutoConfigureWebMvc auto-configuration imports
116117
org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureWebMvc=\
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2012-2017 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+
* http://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.web.reactive.function.client;
18+
19+
import org.springframework.web.reactive.function.client.WebClient;
20+
21+
/**
22+
* Callback interface that can be used to customize a {@link WebClient.Builder}.
23+
*
24+
* @author Brian Clozel
25+
* @since 2.0.0
26+
*/
27+
@FunctionalInterface
28+
public interface WebClientCustomizer {
29+
30+
/**
31+
* Callback to customize a {@link WebClient.Builder} instance.
32+
* @param webClientBuilder the client builder to customize
33+
*/
34+
void customize(WebClient.Builder webClientBuilder);
35+
}

0 commit comments

Comments
 (0)