Skip to content

Commit 9976783

Browse files
Jeoffrey Haeyaertrstoyanchev
Jeoffrey Haeyaert
authored andcommitted
WebClient uri method with uriTemplate and UriBuilder
See gh-22705
1 parent ed70978 commit 9976783

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

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

+6
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,12 @@ public RequestBodySpec uri(String uriTemplate, Map<String, ?> uriVariables) {
184184
return uri(uriBuilderFactory.expand(uriTemplate, uriVariables));
185185
}
186186

187+
@Override
188+
public RequestBodySpec uri(String uriTemplate, Function<UriBuilder, URI> uriFunction) {
189+
attribute(URI_TEMPLATE_ATTRIBUTE, uriTemplate);
190+
return uri(uriFunction.apply(uriBuilderFactory.uriString(uriTemplate)));
191+
}
192+
187193
@Override
188194
public RequestBodySpec uri(Function<UriBuilder, URI> uriFunction) {
189195
return uri(uriFunction.apply(uriBuilderFactory.builder()));

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

+7
Original file line numberDiff line numberDiff line change
@@ -348,9 +348,16 @@ interface UriSpec<S extends RequestHeadersSpec<?>> {
348348
*/
349349
S uri(String uri, Map<String, ?> uriVariables);
350350

351+
/**
352+
* Build the URI for the request using the {@link UriBuilderFactory}
353+
* configured for this client and initialized with the specified <code>uri</code>.
354+
*/
355+
S uri(String uri, Function<UriBuilder, URI> uriFunction);
356+
351357
/**
352358
* Build the URI for the request using the {@link UriBuilderFactory}
353359
* configured for this client.
360+
* @see #uri(String, Function)
354361
*/
355362
S uri(Function<UriBuilder, URI> uriFunction);
356363
}

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

+12
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,18 @@ public void uriBuilder() {
8484
assertEquals("/base/path?q=12", request.url().toString());
8585
}
8686

87+
88+
@Test
89+
public void uriBuilderWithUriTemplate() {
90+
this.builder.build().get()
91+
.uri("/path/{id}", builder -> builder.queryParam("q", "12").build("identifier"))
92+
.exchange().block(Duration.ofSeconds(10));
93+
94+
ClientRequest request = verifyAndGetRequest();
95+
assertEquals("/base/path/identifier?q=12", request.url().toString());
96+
assertEquals("/path/{id}", request.attribute(WebClient.class.getName() + ".uriTemplate").<String>get());
97+
}
98+
8799
@Test
88100
public void uriBuilderWithPathOverride() {
89101
this.builder.build().get()

0 commit comments

Comments
 (0)