Skip to content

Commit b72ee5f

Browse files
committed
Add static factory methods to WebClientAdapter
Ideally one would pass WebClient directly to HttpServiceProxyFactory, but two need to remain decoupled. This commit adds static, shortcut methods to WebClientAdapter to create an HttpServiceProxyFactory, thus eliminating the step to wrap the WebClient.
1 parent 45ee791 commit b72ee5f

File tree

4 files changed

+53
-14
lines changed

4 files changed

+53
-14
lines changed

spring-web/src/main/java/org/springframework/web/service/invoker/HttpServiceProxyFactory.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
*
5151
* @author Rossen Stoyanchev
5252
* @since 6.0
53+
* @see org.springframework.web.reactive.function.client.support.WebClientAdapter
5354
*/
5455
public final class HttpServiceProxyFactory implements InitializingBean, EmbeddedValueResolverAware {
5556

@@ -75,6 +76,7 @@ public final class HttpServiceProxyFactory implements InitializingBean, Embedded
7576
/**
7677
* Create an instance with the underlying HTTP client to use.
7778
* @param clientAdapter an adapter for the client
79+
* @see org.springframework.web.reactive.function.client.support.WebClientAdapter#createHttpServiceProxyFactory(org.springframework.web.reactive.function.client.WebClient)
7880
*/
7981
public HttpServiceProxyFactory(HttpClientAdapter clientAdapter) {
8082
Assert.notNull(clientAdapter, "HttpClientAdapter is required");

spring-webflux/src/main/java/org/springframework/web/reactive/function/client/support/WebClientAdapter.java

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,28 @@
2929
import org.springframework.web.reactive.function.client.WebClient;
3030
import org.springframework.web.service.invoker.HttpClientAdapter;
3131
import org.springframework.web.service.invoker.HttpRequestValues;
32+
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
3233

3334

3435
/**
35-
* {@link HttpClientAdapter} implementation for {@link WebClient}.
36+
* {@link HttpClientAdapter} that enables an {@link HttpServiceProxyFactory} to
37+
* use {@link WebClient} for request execution.
38+
*
39+
* <p>Use static factory methods in this class to create an
40+
* {@code HttpServiceProxyFactory} configured with a given {@code WebClient}.
3641
*
3742
* @author Rossen Stoyanchev
3843
* @since 6.0
3944
*/
40-
public class WebClientAdapter implements HttpClientAdapter {
45+
public final class WebClientAdapter implements HttpClientAdapter {
4146

4247
private final WebClient webClient;
4348

4449

45-
public WebClientAdapter(WebClient webClient) {
50+
/**
51+
* Package private constructor. See static factory methods.
52+
*/
53+
private WebClientAdapter(WebClient webClient) {
4654
this.webClient = webClient;
4755
}
4856

@@ -116,4 +124,38 @@ else if (requestValues.getBody() != null) {
116124
return bodySpec;
117125
}
118126

127+
128+
/**
129+
* Static method to create a {@link HttpServiceProxyFactory} configured to
130+
* use the given {@link WebClient} instance. Effectively a shortcut for:
131+
* <pre>
132+
* WebClientAdapter adapter = WebClientAdapter.forClient(webClient);
133+
* HttpServiceProxyFactory proxyFactory = new HttpServiceProxyFactory(adapter);
134+
* </pre>
135+
* @param webClient the client to use
136+
* @return the created {@code HttpServiceProxyFactory} instance
137+
*/
138+
public static HttpServiceProxyFactory createHttpServiceProxyFactory(WebClient webClient) {
139+
return new HttpServiceProxyFactory(new WebClientAdapter(webClient));
140+
}
141+
142+
/**
143+
* Variant of {@link #createHttpServiceProxyFactory(WebClient)} that accepts
144+
* a {@link WebClient.Builder} and uses it to create the client.
145+
* @param webClientBuilder a builder to create the client to use with
146+
* @return the created {@code HttpServiceProxyFactory} instance
147+
*/
148+
public static HttpServiceProxyFactory createHttpServiceProxyFactory(WebClient.Builder webClientBuilder) {
149+
return createHttpServiceProxyFactory(webClientBuilder.build());
150+
}
151+
152+
/**
153+
* Create a {@link WebClientAdapter} for the given {@code WebClient} instance.
154+
* @param webClient the client to use
155+
* @return the created adapter instance
156+
*/
157+
public static WebClientAdapter forClient(WebClient webClient) {
158+
return new WebClientAdapter(webClient);
159+
}
160+
119161
}

spring-webflux/src/test/java/org/springframework/web/reactive/function/client/support/WebClientHttpServiceProxyTests.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,9 @@ private TestHttpService initHttpService() throws Exception {
106106
}
107107

108108
private TestHttpService initHttpService(WebClient webClient) throws Exception {
109-
WebClientAdapter client = new WebClientAdapter(webClient);
110-
HttpServiceProxyFactory proxyFactory = new HttpServiceProxyFactory(client);
111-
proxyFactory.afterPropertiesSet();
112-
return proxyFactory.createClient(TestHttpService.class);
109+
HttpServiceProxyFactory factory = WebClientAdapter.createHttpServiceProxyFactory(webClient);
110+
factory.afterPropertiesSet();
111+
return factory.createClient(TestHttpService.class);
113112
}
114113

115114
private void prepareResponse(Consumer<MockResponse> consumer) {

src/docs/asciidoc/integration.adoc

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -390,14 +390,10 @@ the `WebClient`:
390390

391391
[source,java,indent=0,subs="verbatim,quotes"]
392392
----
393-
WebClient client = WebClient.builder()
394-
.baseUrl("https://api.github.com/")
395-
.build();
396-
397-
HttpServiceProxyFactory proxyFactory =
398-
HttpServiceProxyFactory.builder(new WebClientAdapter(client)).build();
393+
WebClient client = WebClient.builder().baseUrl("https://api.github.com/").build();
394+
HttpServiceProxyFactory factory = WebClientAdapter.createHttpServiceProxyFactory(client)).build();
399395
400-
RepositoryService service = proxyFactory.createClient(RepositoryService.class);
396+
RepositoryService service = factory.createClient(RepositoryService.class);
401397
----
402398

403399
An HTTP service interface can declare common attributes at the type level:

0 commit comments

Comments
 (0)