diff --git a/framework-docs/modules/ROOT/pages/integration/rest-clients.adoc b/framework-docs/modules/ROOT/pages/integration/rest-clients.adoc index 14f12899de..0ce1a984b0 100644 --- a/framework-docs/modules/ROOT/pages/integration/rest-clients.adoc +++ b/framework-docs/modules/ROOT/pages/integration/rest-clients.adoc @@ -957,9 +957,8 @@ method parameters: | Dynamically set the URL for the request, overriding the annotation's `url` attribute. | `UriBuilderFactory` -| Provide a `UriBuilderFactory` to use to expand the `UriTemplate`. - Allows dynamically setting the base URI for the request, - while maintaining the `path` specified through annotations. +| Provide a `UriBuilderFactory` to expand the URI template and URI variables with. + In effect, replaces the `UriBuilderFactory` (and its base URL) of the underlying client. | `HttpMethod` | Dynamically set the HTTP method for the request, overriding the annotation's `method` attribute diff --git a/spring-web/src/main/java/org/springframework/web/client/support/RestClientAdapter.java b/spring-web/src/main/java/org/springframework/web/client/support/RestClientAdapter.java index bea86fb3ae..1c0377784a 100644 --- a/spring-web/src/main/java/org/springframework/web/client/support/RestClientAdapter.java +++ b/spring-web/src/main/java/org/springframework/web/client/support/RestClientAdapter.java @@ -30,6 +30,7 @@ import org.springframework.web.client.RestClient; import org.springframework.web.service.invoker.HttpExchangeAdapter; import org.springframework.web.service.invoker.HttpRequestValues; import org.springframework.web.service.invoker.HttpServiceProxyFactory; +import org.springframework.web.util.UriBuilderFactory; /** * {@link HttpExchangeAdapter} that enables an {@link HttpServiceProxyFactory} @@ -93,19 +94,16 @@ public final class RestClientAdapter implements HttpExchangeAdapter { if (values.getUri() != null) { bodySpec = uriSpec.uri(values.getUri()); } - else if (values.getUriTemplate() != null) { - if (values.getUriBuilderFactory() != null) { - URI expanded = values.getUriBuilderFactory() - .expand(values.getUriTemplate(), values.getUriVariables()); - bodySpec = uriSpec.uri(expanded); + UriBuilderFactory uriBuilderFactory = values.getUriBuilderFactory(); + if (uriBuilderFactory != null) { + URI uri = uriBuilderFactory.expand(values.getUriTemplate(), values.getUriVariables()); + bodySpec = uriSpec.uri(uri); } - else { bodySpec = uriSpec.uri(values.getUriTemplate(), values.getUriVariables()); } } - else { throw new IllegalStateException("Neither full URL nor URI template"); } diff --git a/spring-web/src/main/java/org/springframework/web/client/support/RestTemplateAdapter.java b/spring-web/src/main/java/org/springframework/web/client/support/RestTemplateAdapter.java index 72064cdb96..3cd2b927db 100644 --- a/spring-web/src/main/java/org/springframework/web/client/support/RestTemplateAdapter.java +++ b/spring-web/src/main/java/org/springframework/web/client/support/RestTemplateAdapter.java @@ -31,6 +31,7 @@ import org.springframework.web.client.RestTemplate; import org.springframework.web.service.invoker.HttpExchangeAdapter; import org.springframework.web.service.invoker.HttpRequestValues; import org.springframework.web.service.invoker.HttpServiceProxyFactory; +import org.springframework.web.util.UriBuilderFactory; /** * {@link HttpExchangeAdapter} that enables an {@link HttpServiceProxyFactory} @@ -92,19 +93,16 @@ public final class RestTemplateAdapter implements HttpExchangeAdapter { if (values.getUri() != null) { builder = RequestEntity.method(httpMethod, values.getUri()); } - else if (values.getUriTemplate() != null) { - if (values.getUriBuilderFactory() != null) { - URI expanded = values.getUriBuilderFactory() - .expand(values.getUriTemplate(), values.getUriVariables()); + UriBuilderFactory uriBuilderFactory = values.getUriBuilderFactory(); + if (uriBuilderFactory != null) { + URI expanded = uriBuilderFactory.expand(values.getUriTemplate(), values.getUriVariables()); builder = RequestEntity.method(httpMethod, expanded); } - else { builder = RequestEntity.method(httpMethod, values.getUriTemplate(), values.getUriVariables()); } } - else { throw new IllegalStateException("Neither full URL nor URI template"); } diff --git a/spring-web/src/main/java/org/springframework/web/service/invoker/HttpRequestValues.java b/spring-web/src/main/java/org/springframework/web/service/invoker/HttpRequestValues.java index 7281cf4457..1a5fcea560 100644 --- a/spring-web/src/main/java/org/springframework/web/service/invoker/HttpRequestValues.java +++ b/spring-web/src/main/java/org/springframework/web/service/invoker/HttpRequestValues.java @@ -63,10 +63,10 @@ public class HttpRequestValues { private final URI uri; @Nullable - private final String uriTemplate; + private final UriBuilderFactory uriBuilderFactory; @Nullable - private final UriBuilderFactory uriBuilderFactory; + private final String uriTemplate; private final Map uriVariables; @@ -81,11 +81,10 @@ public class HttpRequestValues { /** - * Construct {@link HttpRequestValues}. - * - * @deprecated in favour of {@link HttpRequestValues#HttpRequestValues( - * HttpMethod, URI, String, UriBuilderFactory, Map, HttpHeaders, - * MultiValueMap, Map, Object)} to be removed in 6.2. + * Constructor without UriBuilderFactory. + * @deprecated in favour of + * {@link HttpRequestValues#HttpRequestValues(HttpMethod, URI, UriBuilderFactory, String, Map, HttpHeaders, MultiValueMap, Map, Object)} + * to be removed in 6.2. */ @Deprecated(since = "6.1", forRemoval = true) protected HttpRequestValues(@Nullable HttpMethod httpMethod, @@ -94,13 +93,16 @@ public class HttpRequestValues { HttpHeaders headers, MultiValueMap cookies, Map attributes, @Nullable Object bodyValue) { - this(httpMethod, uri, uriTemplate, null, uriVariables, - headers, cookies, attributes, bodyValue); + this(httpMethod, uri, null, uriTemplate, uriVariables, headers, cookies, attributes, bodyValue); } + /** + * Construct {@link HttpRequestValues}. + * @since 6.1 + */ protected HttpRequestValues(@Nullable HttpMethod httpMethod, - @Nullable URI uri, @Nullable String uriTemplate, - @Nullable UriBuilderFactory uriBuilderFactory, Map uriVariables, + @Nullable URI uri, @Nullable UriBuilderFactory uriBuilderFactory, + @Nullable String uriTemplate, Map uriVariables, HttpHeaders headers, MultiValueMap cookies, Map attributes, @Nullable Object bodyValue) { @@ -108,8 +110,8 @@ public class HttpRequestValues { this.httpMethod = httpMethod; this.uri = uri; - this.uriTemplate = uriTemplate; this.uriBuilderFactory = uriBuilderFactory; + this.uriTemplate = uriTemplate; this.uriVariables = uriVariables; this.headers = headers; this.cookies = cookies; @@ -137,6 +139,19 @@ public class HttpRequestValues { return this.uri; } + /** + * Return the {@link UriBuilderFactory} to expand + * the {@link HttpRequestValues#uriTemplate} and {@link #getUriVariables()} with. + *

The {@link UriBuilderFactory} is passed into the HTTP interface method + * in order to override the UriBuilderFactory (and its baseUrl) used by the + * underlying client. + * @since 6.1 + */ + @Nullable + public UriBuilderFactory getUriBuilderFactory() { + return this.uriBuilderFactory; + } + /** * Return the URL template for the request. This comes from the values in * class and method {@code HttpExchange} annotations. @@ -146,19 +161,6 @@ public class HttpRequestValues { return this.uriTemplate; } - /** - * Return the {@link UriBuilderFactory} to expand - * the {@link HttpRequestValues#uriTemplate} with. - *

This comes from a {@link UriBuilderFactory} method argument. - * It allows you to override the {@code baseUri}, while keeping the - * path as defined in class and method - * {@code HttpExchange} annotations. - */ - @Nullable - public UriBuilderFactory getUriBuilderFactory() { - return this.uriBuilderFactory; - } - /** * Return the URL template variables, or an empty map. */ @@ -237,10 +239,10 @@ public class HttpRequestValues { private URI uri; @Nullable - private String uriTemplate; + private UriBuilderFactory uriBuilderFactory; @Nullable - private UriBuilderFactory uriBuilderFactory; + private String uriTemplate; @Nullable private Map uriVars; @@ -282,19 +284,20 @@ public class HttpRequestValues { } /** - * Set the request URL as a String template. + * Set the {@link UriBuilderFactory} that will be used to expand the + * {@link #getUriTemplate()}. + * @since 6.1 */ - public Builder setUriTemplate(String uriTemplate) { - this.uriTemplate = uriTemplate; + public Builder setUriBuilderFactory(@Nullable UriBuilderFactory uriBuilderFactory) { + this.uriBuilderFactory = uriBuilderFactory; return this; } /** - * Set the {@link UriBuilderFactory} that - * will be used to expand the URI. + * Set the request URL as a String template. */ - public Builder setUriBuilderFactory(@Nullable UriBuilderFactory uriBuilderFactory) { - this.uriBuilderFactory = uriBuilderFactory; + public Builder setUriTemplate(String uriTemplate) { + this.uriTemplate = uriTemplate; return this; } @@ -427,8 +430,8 @@ public class HttpRequestValues { public HttpRequestValues build() { URI uri = this.uri; - String uriTemplate = (this.uriTemplate != null ? this.uriTemplate : ""); UriBuilderFactory uriBuilderFactory = this.uriBuilderFactory; + String uriTemplate = (this.uriTemplate != null ? this.uriTemplate : ""); Map uriVars = (this.uriVars != null ? new HashMap<>(this.uriVars) : Collections.emptyMap()); Object bodyValue = this.bodyValue; @@ -470,8 +473,8 @@ public class HttpRequestValues { new HashMap<>(this.attributes) : Collections.emptyMap()); return createRequestValues( - this.httpMethod, uri, uriTemplate, uriBuilderFactory, - uriVars, headers, cookies, attributes, bodyValue); + this.httpMethod, uri, uriBuilderFactory, uriTemplate, uriVars, + headers, cookies, attributes, bodyValue); } protected boolean hasParts() { @@ -512,9 +515,9 @@ public class HttpRequestValues { /** * Create {@link HttpRequestValues} from values passed to the {@link Builder}. - * @deprecated in favour of {@link Builder#createRequestValues( - * HttpMethod, URI, String, UriBuilderFactory, Map, HttpHeaders, - * MultiValueMap, Map, Object)} to be removed in 6.2. + * @deprecated in favour of + * {@link Builder#createRequestValues(HttpMethod, URI, UriBuilderFactory, String, Map, HttpHeaders, MultiValueMap, Map, Object)} + * to be removed in 6.2. */ @Deprecated(since = "6.1", forRemoval = true) protected HttpRequestValues createRequestValues( @@ -524,22 +527,23 @@ public class HttpRequestValues { HttpHeaders headers, MultiValueMap cookies, Map attributes, @Nullable Object bodyValue) { - return createRequestValues(httpMethod, uri, uriTemplate, null, + return createRequestValues(httpMethod, uri, null, uriTemplate, uriVars, headers, cookies, attributes, bodyValue); } /** * Create {@link HttpRequestValues} from values passed to the {@link Builder}. + * @since 6.1 */ protected HttpRequestValues createRequestValues( @Nullable HttpMethod httpMethod, - @Nullable URI uri, @Nullable String uriTemplate, - @Nullable UriBuilderFactory uriBuilderFactory, Map uriVars, + @Nullable URI uri, @Nullable UriBuilderFactory uriBuilderFactory, @Nullable String uriTemplate, + Map uriVars, HttpHeaders headers, MultiValueMap cookies, Map attributes, @Nullable Object bodyValue) { return new HttpRequestValues( - this.httpMethod, uri, uriTemplate, uriBuilderFactory, + this.httpMethod, uri, uriBuilderFactory, uriTemplate, uriVars, headers, cookies, attributes, bodyValue); } } diff --git a/spring-web/src/main/java/org/springframework/web/service/invoker/ReactiveHttpRequestValues.java b/spring-web/src/main/java/org/springframework/web/service/invoker/ReactiveHttpRequestValues.java index 182cd2a183..e485c53fce 100644 --- a/spring-web/src/main/java/org/springframework/web/service/invoker/ReactiveHttpRequestValues.java +++ b/spring-web/src/main/java/org/springframework/web/service/invoker/ReactiveHttpRequestValues.java @@ -51,14 +51,12 @@ public final class ReactiveHttpRequestValues extends HttpRequestValues { private ReactiveHttpRequestValues( @Nullable HttpMethod httpMethod, - @Nullable URI uri, @Nullable String uriTemplate, - @Nullable UriBuilderFactory uriBuilderFactory, Map uriVariables, + @Nullable URI uri, @Nullable UriBuilderFactory uriBuilderFactory, + @Nullable String uriTemplate, Map uriVars, HttpHeaders headers, MultiValueMap cookies, Map attributes, @Nullable Object bodyValue, @Nullable Publisher body, @Nullable ParameterizedTypeReference elementType) { - super(httpMethod, uri, uriTemplate, uriBuilderFactory, - uriVariables, headers, cookies, attributes, bodyValue); - + super(httpMethod, uri, uriBuilderFactory, uriTemplate, uriVars, headers, cookies, attributes, bodyValue); this.body = body; this.bodyElementType = elementType; } @@ -135,14 +133,14 @@ public final class ReactiveHttpRequestValues extends HttpRequestValues { } @Override - public Builder setUriTemplate(String uriTemplate) { - super.setUriTemplate(uriTemplate); + public Builder setUriBuilderFactory(@Nullable UriBuilderFactory uriBuilderFactory) { + super.setUriBuilderFactory(uriBuilderFactory); return this; } @Override - public Builder setUriBuilderFactory(UriBuilderFactory uriBuilderFactory) { - super.setUriBuilderFactory(uriBuilderFactory); + public Builder setUriTemplate(String uriTemplate) { + super.setUriTemplate(uriTemplate); return this; } @@ -271,17 +269,15 @@ public final class ReactiveHttpRequestValues extends HttpRequestValues { @Override protected ReactiveHttpRequestValues createRequestValues( @Nullable HttpMethod httpMethod, - @Nullable URI uri, @Nullable String uriTemplate, - @Nullable UriBuilderFactory uriBuilderFactory, Map uriVars, + @Nullable URI uri, @Nullable UriBuilderFactory uriBuilderFactory, + @Nullable String uriTemplate, Map uriVars, HttpHeaders headers, MultiValueMap cookies, Map attributes, @Nullable Object bodyValue) { return new ReactiveHttpRequestValues( - httpMethod, uri, uriTemplate, uriBuilderFactory, - uriVars, headers, cookies, attributes, - bodyValue, this.body, this.bodyElementType); + httpMethod, uri, uriBuilderFactory, uriTemplate, uriVars, + headers, cookies, attributes, bodyValue, this.body, this.bodyElementType); } - } } diff --git a/spring-web/src/test/java/org/springframework/web/client/support/RestClientAdapterTests.java b/spring-web/src/test/java/org/springframework/web/client/support/RestClientAdapterTests.java index dfc0415669..78f8b33109 100644 --- a/spring-web/src/test/java/org/springframework/web/client/support/RestClientAdapterTests.java +++ b/spring-web/src/test/java/org/springframework/web/client/support/RestClientAdapterTests.java @@ -74,6 +74,7 @@ class RestClientAdapterTests { private final MockWebServer anotherServer = anotherServer(); + @SuppressWarnings("ConstantValue") @AfterEach void shutdown() throws IOException { @@ -121,19 +122,24 @@ class RestClientAdapterTests { @ParameterizedAdapterTest - void greeting(MockWebServer server, Service service, TestObservationRegistry observationRegistry) throws Exception { + void greeting( + MockWebServer server, Service service, TestObservationRegistry observationRegistry) throws Exception { + String response = service.getGreeting(); RecordedRequest request = server.takeRequest(); assertThat(response).isEqualTo("Hello Spring!"); assertThat(request.getMethod()).isEqualTo("GET"); assertThat(request.getPath()).isEqualTo("/greeting"); - TestObservationRegistryAssert.assertThat(observationRegistry).hasObservationWithNameEqualTo("http.client.requests") + TestObservationRegistryAssert.assertThat(observationRegistry) + .hasObservationWithNameEqualTo("http.client.requests") .that().hasLowCardinalityKeyValue("uri", "/greeting"); } @ParameterizedAdapterTest - void greetingById(MockWebServer server, Service service, TestObservationRegistry observationRegistry) throws Exception { + void greetingById( + MockWebServer server, Service service, TestObservationRegistry observationRegistry) throws Exception { + ResponseEntity response = service.getGreetingById("456"); RecordedRequest request = server.takeRequest(); @@ -141,12 +147,15 @@ class RestClientAdapterTests { assertThat(response.getBody()).isEqualTo("Hello Spring!"); assertThat(request.getMethod()).isEqualTo("GET"); assertThat(request.getPath()).isEqualTo("/greeting/456"); - TestObservationRegistryAssert.assertThat(observationRegistry).hasObservationWithNameEqualTo("http.client.requests") + TestObservationRegistryAssert.assertThat(observationRegistry) + .hasObservationWithNameEqualTo("http.client.requests") .that().hasLowCardinalityKeyValue("uri", "/greeting/{id}"); } @ParameterizedAdapterTest - void greetingWithDynamicUri(MockWebServer server, Service service, TestObservationRegistry observationRegistry) throws Exception { + void greetingWithDynamicUri( + MockWebServer server, Service service, TestObservationRegistry observationRegistry) throws Exception { + URI dynamicUri = server.url("/greeting/123").uri(); Optional response = service.getGreetingWithDynamicUri(dynamicUri, "456"); @@ -154,7 +163,8 @@ class RestClientAdapterTests { assertThat(response.orElse("empty")).isEqualTo("Hello Spring!"); assertThat(request.getMethod()).isEqualTo("GET"); assertThat(request.getRequestUrl().uri()).isEqualTo(dynamicUri); - TestObservationRegistryAssert.assertThat(observationRegistry).hasObservationWithNameEqualTo("http.client.requests") + TestObservationRegistryAssert.assertThat(observationRegistry) + .hasObservationWithNameEqualTo("http.client.requests") .that().hasLowCardinalityKeyValue("uri", "none"); } @@ -218,8 +228,8 @@ class RestClientAdapterTests { @ParameterizedAdapterTest void getWithUriBuilderFactory(MockWebServer server, Service service) throws InterruptedException { - UriBuilderFactory factory = new DefaultUriBuilderFactory(this.anotherServer.url("/") - .toString()); + String url = this.anotherServer.url("/").toString(); + UriBuilderFactory factory = new DefaultUriBuilderFactory(url); ResponseEntity actualResponse = service.getWithUriBuilderFactory(factory); @@ -233,11 +243,10 @@ class RestClientAdapterTests { @ParameterizedAdapterTest void getWithFactoryPathVariableAndRequestParam(MockWebServer server, Service service) throws InterruptedException { - UriBuilderFactory factory = new DefaultUriBuilderFactory(this.anotherServer.url("/") - .toString()); + String url = this.anotherServer.url("/").toString(); + UriBuilderFactory factory = new DefaultUriBuilderFactory(url); - ResponseEntity actualResponse = service.getWithUriBuilderFactory(factory, "123", - "test"); + ResponseEntity actualResponse = service.getWithUriBuilderFactory(factory, "123", "test"); RecordedRequest request = this.anotherServer.takeRequest(); assertThat(actualResponse.getStatusCode()).isEqualTo(HttpStatus.OK); @@ -250,8 +259,7 @@ class RestClientAdapterTests { @ParameterizedAdapterTest void getWithIgnoredUriBuilderFactory(MockWebServer server, Service service) throws InterruptedException { URI dynamicUri = server.url("/greeting/123").uri(); - UriBuilderFactory factory = new DefaultUriBuilderFactory(this.anotherServer.url("/") - .toString()); + UriBuilderFactory factory = new DefaultUriBuilderFactory(this.anotherServer.url("/").toString()); ResponseEntity actualResponse = service.getWithIgnoredUriBuilderFactory(dynamicUri, factory); @@ -265,11 +273,11 @@ class RestClientAdapterTests { private static MockWebServer anotherServer() { - MockWebServer anotherServer = new MockWebServer(); + MockWebServer server = new MockWebServer(); MockResponse response = new MockResponse(); response.setHeader("Content-Type", "text/plain").setBody("Hello Spring 2!"); - anotherServer.enqueue(response); - return anotherServer; + server.enqueue(response); + return server; } diff --git a/spring-web/src/test/java/org/springframework/web/service/invoker/UriBuilderFactoryArgumentResolverTests.java b/spring-web/src/test/java/org/springframework/web/service/invoker/UriBuilderFactoryArgumentResolverTests.java index ab19a4f40c..71ae0a28e2 100644 --- a/spring-web/src/test/java/org/springframework/web/service/invoker/UriBuilderFactoryArgumentResolverTests.java +++ b/spring-web/src/test/java/org/springframework/web/service/invoker/UriBuilderFactoryArgumentResolverTests.java @@ -35,8 +35,8 @@ class UriBuilderFactoryArgumentResolverTests { private final TestExchangeAdapter client = new TestExchangeAdapter(); private final Service service = - HttpServiceProxyFactory.builderFor(this.client).build() - .createClient(Service.class); + HttpServiceProxyFactory.builderFor(this.client).build().createClient(Service.class); + @Test void uriBuilderFactory(){ diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/support/WebClientAdapter.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/support/WebClientAdapter.java index b8f297c232..a344bf20a8 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/support/WebClientAdapter.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/support/WebClientAdapter.java @@ -33,6 +33,7 @@ import org.springframework.web.service.invoker.HttpRequestValues; import org.springframework.web.service.invoker.HttpServiceProxyFactory; import org.springframework.web.service.invoker.ReactiveHttpRequestValues; import org.springframework.web.service.invoker.ReactorHttpExchangeAdapter; +import org.springframework.web.util.UriBuilderFactory; /** * {@link ReactorHttpExchangeAdapter} that enables an {@link HttpServiceProxyFactory} @@ -111,12 +112,11 @@ public final class WebClientAdapter extends AbstractReactorHttpExchangeAdapter { } else if (values.getUriTemplate() != null) { - if(values.getUriBuilderFactory() != null){ - URI expanded = values.getUriBuilderFactory() - .expand(values.getUriTemplate(), values.getUriVariables()); - bodySpec = uriSpec.uri(expanded); + UriBuilderFactory uriBuilderFactory = values.getUriBuilderFactory(); + if(uriBuilderFactory != null){ + URI uri = uriBuilderFactory.expand(values.getUriTemplate(), values.getUriVariables()); + bodySpec = uriSpec.uri(uri); } - else { bodySpec = uriSpec.uri(values.getUriTemplate(), values.getUriVariables()); } diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/support/WebClientAdapterTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/support/WebClientAdapterTests.java index c8d0c696e0..db58ab3546 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/support/WebClientAdapterTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/support/WebClientAdapterTests.java @@ -172,8 +172,7 @@ public class WebClientAdapterTests { void uriBuilderFactory() throws Exception { String ignoredResponseBody = "hello"; prepareResponse(response -> response.setResponseCode(200).setBody(ignoredResponseBody)); - UriBuilderFactory factory = new DefaultUriBuilderFactory(this.anotherServer.url("/") - .toString()); + UriBuilderFactory factory = new DefaultUriBuilderFactory(this.anotherServer.url("/").toString()); String actualBody = initService().getWithUriBuilderFactory(factory); @@ -186,14 +185,12 @@ public class WebClientAdapterTests { void uriBuilderFactoryWithPathVariableAndRequestParam() throws Exception { String ignoredResponseBody = "hello"; prepareResponse(response -> response.setResponseCode(200).setBody(ignoredResponseBody)); - UriBuilderFactory factory = new DefaultUriBuilderFactory(this.anotherServer.url("/") - .toString()); + UriBuilderFactory factory = new DefaultUriBuilderFactory(this.anotherServer.url("/").toString()); String actualBody = initService().getWithUriBuilderFactory(factory, "123", "test"); assertThat(actualBody).isEqualTo(ANOTHER_SERVER_RESPONSE_BODY); - assertThat(this.anotherServer.takeRequest().getPath()) - .isEqualTo("/greeting/123?param=test"); + assertThat(this.anotherServer.takeRequest().getPath()).isEqualTo("/greeting/123?param=test"); assertThat(this.server.getRequestCount()).isEqualTo(0); } @@ -202,8 +199,7 @@ public class WebClientAdapterTests { String expectedResponseBody = "hello"; prepareResponse(response -> response.setResponseCode(200).setBody(expectedResponseBody)); URI dynamicUri = this.server.url("/greeting/123").uri(); - UriBuilderFactory factory = new DefaultUriBuilderFactory(this.anotherServer.url("/") - .toString()); + UriBuilderFactory factory = new DefaultUriBuilderFactory(this.anotherServer.url("/").toString()); String actualBody = initService().getWithIgnoredUriBuilderFactory(dynamicUri, factory); @@ -216,8 +212,7 @@ public class WebClientAdapterTests { private static MockWebServer anotherServer() { MockWebServer anotherServer = new MockWebServer(); MockResponse response = new MockResponse(); - response.setHeader("Content-Type", "text/plain") - .setBody(ANOTHER_SERVER_RESPONSE_BODY); + response.setHeader("Content-Type", "text/plain").setBody(ANOTHER_SERVER_RESPONSE_BODY); anotherServer.enqueue(response); return anotherServer; } diff --git a/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/client/support/WebClientHttpServiceProxyKotlinTests.kt b/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/client/support/WebClientHttpServiceProxyKotlinTests.kt index abecdadc56..85de7f5a24 100644 --- a/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/client/support/WebClientHttpServiceProxyKotlinTests.kt +++ b/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/client/support/WebClientHttpServiceProxyKotlinTests.kt @@ -135,17 +135,12 @@ class KotlinWebClientHttpServiceProxyTests { @Throws(InterruptedException::class) fun getWithFactoryPathVariableAndRequestParam() { prepareResponse { response: MockResponse -> - response.setHeader( - "Content-Type", - "text/plain" - ).setBody("Hello Spring!") + response.setHeader("Content-Type", "text/plain").setBody("Hello Spring!") } - val factory: UriBuilderFactory = DefaultUriBuilderFactory(anotherServer.url("/") - .toString()) + val factory: UriBuilderFactory = DefaultUriBuilderFactory(anotherServer.url("/").toString()) - val actualResponse: ResponseEntity = initHttpService() - .getWithUriBuilderFactory(factory, "123", - "test") + val actualResponse: ResponseEntity = + initHttpService().getWithUriBuilderFactory(factory, "123", "test") val request = anotherServer.takeRequest() assertThat(actualResponse.statusCode).isEqualTo(HttpStatus.OK) @@ -159,17 +154,13 @@ class KotlinWebClientHttpServiceProxyTests { @Throws(InterruptedException::class) fun getWithIgnoredUriBuilderFactory() { prepareResponse { response: MockResponse -> - response.setHeader( - "Content-Type", - "text/plain" - ).setBody("Hello Spring!") + response.setHeader("Content-Type", "text/plain").setBody("Hello Spring!") } val dynamicUri = server.url("/greeting/123").uri() - val factory: UriBuilderFactory = DefaultUriBuilderFactory(anotherServer.url("/") - .toString()) + val factory: UriBuilderFactory = DefaultUriBuilderFactory(anotherServer.url("/").toString()) - val actualResponse: ResponseEntity = initHttpService() - .getWithIgnoredUriBuilderFactory(dynamicUri, factory) + val actualResponse: ResponseEntity = + initHttpService().getWithIgnoredUriBuilderFactory(dynamicUri, factory) val request = server.takeRequest() assertThat(actualResponse.statusCode).isEqualTo(HttpStatus.OK) @@ -181,9 +172,7 @@ class KotlinWebClientHttpServiceProxyTests { private fun initHttpService(): TestHttpService { - val webClient = WebClient.builder().baseUrl( - server.url("/").toString() - ).build() + val webClient = WebClient.builder().baseUrl(server.url("/").toString()).build() return initHttpService(webClient) } @@ -220,8 +209,8 @@ class KotlinWebClientHttpServiceProxyTests { suspend fun getGreetingSuspendingWithAttribute(@RequestAttribute myAttribute: String): String @GetExchange("/greeting/{id}") - fun getWithUriBuilderFactory(uriBuilderFactory: UriBuilderFactory?, - @PathVariable id: String?, @RequestParam param: String?): ResponseEntity + fun getWithUriBuilderFactory( + uriBuilderFactory: UriBuilderFactory?, @PathVariable id: String?, @RequestParam param: String?): ResponseEntity @GetExchange("/greeting") fun getWithIgnoredUriBuilderFactory(uri: URI?, uriBuilderFactory: UriBuilderFactory?): ResponseEntity