Better support for overriding base URI in WebClient

The base URI is ignored for requests that include a host.

WebClient exposes UriBuilder (rather than UriBuilderFactory) for
per-request URI building based on the base URI. That provides
full control to add or replace components of the base URI.
This commit is contained in:
Rossen Stoyanchev
2017-02-02 17:09:53 -05:00
parent 82a34f4b24
commit 1466c82f53
6 changed files with 93 additions and 36 deletions

View File

@@ -167,6 +167,11 @@ public class DefaultUriBuilderFactory implements UriBuilderFactory {
return new DefaultUriBuilder(uriTemplate);
}
@Override
public UriBuilder builder() {
return new DefaultUriBuilder("");
}
/**
* {@link DefaultUriBuilderFactory} specific implementation of UriBuilder.
@@ -182,9 +187,11 @@ public class DefaultUriBuilderFactory implements UriBuilderFactory {
private UriComponentsBuilder initUriComponentsBuilder(String uriTemplate) {
UriComponentsBuilder result = baseUri.cloneBuilder();
UriComponents child = UriComponentsBuilder.fromUriString(uriTemplate).build();
result.uriComponents(child);
UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(uriTemplate);
UriComponents uriComponents = uriComponentsBuilder.build();
UriComponentsBuilder result = (uriComponents.getHost() == null ?
baseUri.cloneBuilder().uriComponents(uriComponents) : uriComponentsBuilder);
if (shouldParsePath()) {
UriComponents uric = result.build();

View File

@@ -30,14 +30,18 @@ package org.springframework.web.util;
public interface UriBuilderFactory extends UriTemplateHandler {
/**
* Return a builder that is initialized with the given URI string which may
* be a URI template and represent full URI or just a path.
* <p>Depending on the factory implementation and configuration, the builder
* may merge the given URI string with a base URI and apply other operations.
* Refer to the specific factory implementation for details.
* @param uriTemplate the URI template to create the builder with
* Return a builder initialized with the given URI string.
* <p>Concrete implementations may apply further initializations such as
* combining with a pre-configured base URI.
* @param uriTemplate the URI template to initialize the builder with
* @return the UriBuilder
*/
UriBuilder uriString(String uriTemplate);
/**
* Return a builder to prepare a new URI.
* @return the UriBuilder
*/
UriBuilder builder();
}