diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java index dd9408d7320..d7ff00edce7 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java @@ -184,6 +184,12 @@ class DefaultWebClient implements WebClient { return uri(uriBuilderFactory.expand(uriTemplate, uriVariables)); } + @Override + public RequestBodySpec uri(String uriTemplate, Function uriFunction) { + attribute(URI_TEMPLATE_ATTRIBUTE, uriTemplate); + return uri(uriFunction.apply(uriBuilderFactory.uriString(uriTemplate))); + } + @Override public RequestBodySpec uri(Function uriFunction) { return uri(uriFunction.apply(uriBuilderFactory.builder())); diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClient.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClient.java index 4f17059bb14..1a7f3660768 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClient.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClient.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -349,8 +349,15 @@ public interface WebClient { S uri(String uri, Map uriVariables); /** - * Build the URI for the request using the {@link UriBuilderFactory} - * configured for this client. + * Specify the URI starting with a URI template and finishing off with a + * {@link UriBuilder} created from the template. + * @since 5.2 + */ + S uri(String uri, Function uriFunction); + + /** + * Specify the URI by through a {@link UriBuilder}. + * @see #uri(String, Function) */ S uri(Function uriFunction); } diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/DefaultWebClientTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/DefaultWebClientTests.java index f62db48c0c9..70c38eee1d4 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/DefaultWebClientTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/DefaultWebClientTests.java @@ -84,6 +84,17 @@ public class DefaultWebClientTests { assertEquals("/base/path?q=12", request.url().toString()); } + @Test // gh-22705 + public void uriBuilderWithUriTemplate() { + this.builder.build().get() + .uri("/path/{id}", builder -> builder.queryParam("q", "12").build("identifier")) + .exchange().block(Duration.ofSeconds(10)); + + ClientRequest request = verifyAndGetRequest(); + assertEquals("/base/path/identifier?q=12", request.url().toString()); + assertEquals("/path/{id}", request.attribute(WebClient.class.getName() + ".uriTemplate").get()); + } + @Test public void uriBuilderWithPathOverride() { this.builder.build().get()