Resolve URI to baseUrl in RestClient
Closes gh-32679
This commit is contained in:
committed by
Arjen Poutsma
parent
98e89d8fba
commit
5864f57198
@@ -341,7 +341,13 @@ final class DefaultRestClient implements RestClient {
|
||||
|
||||
@Override
|
||||
public RequestBodySpec uri(URI uri) {
|
||||
this.uri = uri;
|
||||
if (uri.isAbsolute()) {
|
||||
this.uri = uri;
|
||||
}
|
||||
else {
|
||||
URI baseUri = DefaultRestClient.this.uriBuilderFactory.expand("");
|
||||
this.uri = baseUri.resolve(uri);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.web.client;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedHashMap;
|
||||
@@ -245,6 +246,12 @@ final class DefaultRestClientBuilder implements RestClient.Builder {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RestClient.Builder baseUrl(URI baseUrl) {
|
||||
this.baseUrl = baseUrl.toString();
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RestClient.Builder defaultUriVariables(Map<String, ?> defaultUriVariables) {
|
||||
this.defaultUriVariables = defaultUriVariables;
|
||||
|
||||
@@ -154,6 +154,17 @@ public interface RestClient {
|
||||
return new DefaultRestClientBuilder().baseUrl(baseUrl).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Variant of {@link #create()} that accepts a default base {@code URI}. For more
|
||||
* details see {@link Builder#baseUrl(URI) Builder.baseUrl(URI)}.
|
||||
* @param baseUrl the base URI for all requests
|
||||
* @since 6.2
|
||||
* @see #builder()
|
||||
*/
|
||||
static RestClient create(URI baseUrl) {
|
||||
return new DefaultRestClientBuilder().baseUrl(baseUrl).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@code RestClient} based on the configuration of the given
|
||||
* {@code RestTemplate}.
|
||||
@@ -230,6 +241,26 @@ public interface RestClient {
|
||||
*/
|
||||
Builder baseUrl(String baseUrl);
|
||||
|
||||
/**
|
||||
* Configure a base {@code URI} for requests. Effectively a shortcut for:
|
||||
* <pre class="code">
|
||||
* URI baseUrl = URI.create("https://abc.go.com/v1");
|
||||
* DefaultUriBuilderFactory factory = new DefaultUriBuilderFactory(baseUrl.toString());
|
||||
* RestClient client = RestClient.builder().uriBuilderFactory(factory).build();
|
||||
* </pre>
|
||||
* <p>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.
|
||||
* <p><strong>Note:</strong> this method is mutually exclusive with
|
||||
* {@link #uriBuilderFactory(UriBuilderFactory)}. If both are used, the
|
||||
* {@code baseUrl} value provided here will be ignored.
|
||||
* @return this builder
|
||||
* @since 6.2
|
||||
* @see DefaultUriBuilderFactory#DefaultUriBuilderFactory(String)
|
||||
* @see #uriBuilderFactory(UriBuilderFactory)
|
||||
*/
|
||||
Builder baseUrl(URI baseUrl);
|
||||
|
||||
/**
|
||||
* Configure default URL variable values to use when expanding URI
|
||||
* templates with a {@link Map}. Effectively a shortcut for:
|
||||
@@ -414,7 +445,11 @@ public interface RestClient {
|
||||
interface UriSpec<S extends RequestHeadersSpec<?>> {
|
||||
|
||||
/**
|
||||
* Specify the URI using an absolute, fully constructed {@link URI}.
|
||||
* Specify the URI using a fully constructed {@link URI}.
|
||||
* <p>If the given URI is absolute, it is used as given. If it is
|
||||
* a relative URI, the {@link UriBuilderFactory} configured for
|
||||
* the client (e.g. with a base URI) will be used to
|
||||
* {@linkplain URI#resolve(URI) resolve} the given URI against.
|
||||
*/
|
||||
S uri(URI uri);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user