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 02d0f07bf3f..22566b2decb 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-2019 the original author or authors. + * Copyright 2002-2020 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. @@ -43,6 +43,7 @@ import org.springframework.http.codec.ClientCodecConfigurer; import org.springframework.util.MultiValueMap; import org.springframework.web.reactive.function.BodyInserter; import org.springframework.web.reactive.function.BodyInserters; +import org.springframework.web.util.DefaultUriBuilderFactory; import org.springframework.web.util.UriBuilder; import org.springframework.web.util.UriBuilderFactory; @@ -163,44 +164,20 @@ public interface WebClient { interface Builder { /** - * Configure a base URL for requests performed through the client. - * - *
For example given base URL "https://abc.go.com/v1": - *
- * Mono<Account> result = client.get().uri("/accounts/{id}", 43)
- * .retrieve()
- * .bodyToMono(Account.class);
- *
- * // Result: https://abc.go.com/v1/accounts/43
- *
- * Flux<Account> result = client.get()
- * .uri(builder -> builder.path("/accounts").queryParam("q", "12").build())
- * .retrieve()
- * .bodyToFlux(Account.class);
- *
- * // Result: https://abc.go.com/v1/accounts?q=12
- *
- *
- * The base URL can be overridden with an absolute URI: + * Configure a base URL for requests. Effectively a shortcut for: + *
*
- * Mono<Account> result = client.get().uri("https://xyz.com/path")
- * .retrieve()
- * .bodyToMono(Account.class);
- *
- * // Result: https://xyz.com/path
+ * String baseUrl = "https://abc.go.com/v1";
+ * DefaultUriBuilderFactory factory = new DefaultUriBuilderFactory(baseUrl);
+ * WebClient client = WebClient.builder().uriBuilderFactory(factory).build();
*
- *
- * Or partially overridden with a {@code UriBuilder}: - *
- * Flux<Account> result = client.get()
- * .uri(builder -> builder.replacePath("/v2/accounts").queryParam("q", "12").build())
- * .retrieve()
- * .bodyToFlux(Account.class);
- *
- * // Result: https://abc.com/v2/accounts?q=12
- *
- *
- * @see #defaultUriVariables(Map)
+ * The {@code DefaultUriBuilderFactory} is used to prepare the URL + * for every request with the given base URL, unless the URL request + * for a given URL is absolute in which case the base URL is ignored. + *
Note: this method is mutually exclusive with + * {@link #uriBuilderFactory(UriBuilderFactory)}. If both are used, the + * baseUrl value provided here will be ignored. + * @see DefaultUriBuilderFactory#DefaultUriBuilderFactory(String) * @see #uriBuilderFactory(UriBuilderFactory) */ Builder baseUrl(String baseUrl); @@ -212,11 +189,28 @@ public interface WebClient { * @see #baseUrl(String) * @see #uriBuilderFactory(UriBuilderFactory) */ + /** + * Configure default URL variable values to use when expanding URI + * templates with a {@link Map}. Effectively a shortcut for: + *
+ *
+ * Map<String, ?> defaultVars = ...; + * DefaultUriBuilderFactory factory = new DefaultUriBuilderFactory(); + * factory.setDefaultVariables(defaultVars); + * WebClient client = WebClient.builder().uriBuilderFactory(factory).build(); + *+ *
Note: this method is mutually exclusive with
+ * {@link #uriBuilderFactory(UriBuilderFactory)}. If both are used, the
+ * baseUrl value provided here will be ignored.
+ * @see DefaultUriBuilderFactory#setDefaultUriVariables(Map)
+ * @see #uriBuilderFactory(UriBuilderFactory)
+ */
Builder defaultUriVariables(Map
*