Skip to content

Commit b5b9386

Browse files
committed
Polishing
Closes gh-31413
1 parent 0cd196e commit b5b9386

File tree

10 files changed

+117
-130
lines changed

10 files changed

+117
-130
lines changed

framework-docs/modules/ROOT/pages/integration/rest-clients.adoc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -957,9 +957,8 @@ method parameters:
957957
| Dynamically set the URL for the request, overriding the annotation's `url` attribute.
958958

959959
| `UriBuilderFactory`
960-
| Provide a `UriBuilderFactory` to use to expand the `UriTemplate`.
961-
Allows dynamically setting the base URI for the request,
962-
while maintaining the `path` specified through annotations.
960+
| Provide a `UriBuilderFactory` to expand the URI template and URI variables with.
961+
In effect, replaces the `UriBuilderFactory` (and its base URL) of the underlying client.
963962

964963
| `HttpMethod`
965964
| Dynamically set the HTTP method for the request, overriding the annotation's `method` attribute

spring-web/src/main/java/org/springframework/web/client/support/RestClientAdapter.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.springframework.web.service.invoker.HttpExchangeAdapter;
3131
import org.springframework.web.service.invoker.HttpRequestValues;
3232
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
33+
import org.springframework.web.util.UriBuilderFactory;
3334

3435
/**
3536
* {@link HttpExchangeAdapter} that enables an {@link HttpServiceProxyFactory}
@@ -93,19 +94,16 @@ private RestClient.RequestBodySpec newRequest(HttpRequestValues values) {
9394
if (values.getUri() != null) {
9495
bodySpec = uriSpec.uri(values.getUri());
9596
}
96-
9797
else if (values.getUriTemplate() != null) {
98-
if (values.getUriBuilderFactory() != null) {
99-
URI expanded = values.getUriBuilderFactory()
100-
.expand(values.getUriTemplate(), values.getUriVariables());
101-
bodySpec = uriSpec.uri(expanded);
98+
UriBuilderFactory uriBuilderFactory = values.getUriBuilderFactory();
99+
if (uriBuilderFactory != null) {
100+
URI uri = uriBuilderFactory.expand(values.getUriTemplate(), values.getUriVariables());
101+
bodySpec = uriSpec.uri(uri);
102102
}
103-
104103
else {
105104
bodySpec = uriSpec.uri(values.getUriTemplate(), values.getUriVariables());
106105
}
107106
}
108-
109107
else {
110108
throw new IllegalStateException("Neither full URL nor URI template");
111109
}

spring-web/src/main/java/org/springframework/web/client/support/RestTemplateAdapter.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.springframework.web.service.invoker.HttpExchangeAdapter;
3232
import org.springframework.web.service.invoker.HttpRequestValues;
3333
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
34+
import org.springframework.web.util.UriBuilderFactory;
3435

3536
/**
3637
* {@link HttpExchangeAdapter} that enables an {@link HttpServiceProxyFactory}
@@ -92,19 +93,16 @@ private RequestEntity<?> newRequest(HttpRequestValues values) {
9293
if (values.getUri() != null) {
9394
builder = RequestEntity.method(httpMethod, values.getUri());
9495
}
95-
9696
else if (values.getUriTemplate() != null) {
97-
if (values.getUriBuilderFactory() != null) {
98-
URI expanded = values.getUriBuilderFactory()
99-
.expand(values.getUriTemplate(), values.getUriVariables());
97+
UriBuilderFactory uriBuilderFactory = values.getUriBuilderFactory();
98+
if (uriBuilderFactory != null) {
99+
URI expanded = uriBuilderFactory.expand(values.getUriTemplate(), values.getUriVariables());
100100
builder = RequestEntity.method(httpMethod, expanded);
101101
}
102-
103102
else {
104103
builder = RequestEntity.method(httpMethod, values.getUriTemplate(), values.getUriVariables());
105104
}
106105
}
107-
108106
else {
109107
throw new IllegalStateException("Neither full URL nor URI template");
110108
}

spring-web/src/main/java/org/springframework/web/service/invoker/HttpRequestValues.java

Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ public class HttpRequestValues {
6363
private final URI uri;
6464

6565
@Nullable
66-
private final String uriTemplate;
66+
private final UriBuilderFactory uriBuilderFactory;
6767

6868
@Nullable
69-
private final UriBuilderFactory uriBuilderFactory;
69+
private final String uriTemplate;
7070

7171
private final Map<String, String> uriVariables;
7272

@@ -81,11 +81,10 @@ public class HttpRequestValues {
8181

8282

8383
/**
84-
* Construct {@link HttpRequestValues}.
85-
*
86-
* @deprecated in favour of {@link HttpRequestValues#HttpRequestValues(
87-
* HttpMethod, URI, String, UriBuilderFactory, Map, HttpHeaders,
88-
* MultiValueMap, Map, Object)} to be removed in 6.2.
84+
* Constructor without UriBuilderFactory.
85+
* @deprecated in favour of
86+
* {@link HttpRequestValues#HttpRequestValues(HttpMethod, URI, UriBuilderFactory, String, Map, HttpHeaders, MultiValueMap, Map, Object)}
87+
* to be removed in 6.2.
8988
*/
9089
@Deprecated(since = "6.1", forRemoval = true)
9190
protected HttpRequestValues(@Nullable HttpMethod httpMethod,
@@ -94,22 +93,25 @@ protected HttpRequestValues(@Nullable HttpMethod httpMethod,
9493
HttpHeaders headers, MultiValueMap<String, String> cookies, Map<String, Object> attributes,
9594
@Nullable Object bodyValue) {
9695

97-
this(httpMethod, uri, uriTemplate, null, uriVariables,
98-
headers, cookies, attributes, bodyValue);
96+
this(httpMethod, uri, null, uriTemplate, uriVariables, headers, cookies, attributes, bodyValue);
9997
}
10098

99+
/**
100+
* Construct {@link HttpRequestValues}.
101+
* @since 6.1
102+
*/
101103
protected HttpRequestValues(@Nullable HttpMethod httpMethod,
102-
@Nullable URI uri, @Nullable String uriTemplate,
103-
@Nullable UriBuilderFactory uriBuilderFactory, Map<String, String> uriVariables,
104+
@Nullable URI uri, @Nullable UriBuilderFactory uriBuilderFactory,
105+
@Nullable String uriTemplate, Map<String, String> uriVariables,
104106
HttpHeaders headers, MultiValueMap<String, String> cookies, Map<String, Object> attributes,
105107
@Nullable Object bodyValue) {
106108

107109
Assert.isTrue(uri != null || uriTemplate != null, "Neither URI nor URI template");
108110

109111
this.httpMethod = httpMethod;
110112
this.uri = uri;
111-
this.uriTemplate = uriTemplate;
112113
this.uriBuilderFactory = uriBuilderFactory;
114+
this.uriTemplate = uriTemplate;
113115
this.uriVariables = uriVariables;
114116
this.headers = headers;
115117
this.cookies = cookies;
@@ -138,25 +140,25 @@ public URI getUri() {
138140
}
139141

140142
/**
141-
* Return the URL template for the request. This comes from the values in
142-
* class and method {@code HttpExchange} annotations.
143+
* Return the {@link UriBuilderFactory} to expand
144+
* the {@link HttpRequestValues#uriTemplate} and {@link #getUriVariables()} with.
145+
* <p>The {@link UriBuilderFactory} is passed into the HTTP interface method
146+
* in order to override the UriBuilderFactory (and its baseUrl) used by the
147+
* underlying client.
148+
* @since 6.1
143149
*/
144150
@Nullable
145-
public String getUriTemplate() {
146-
return this.uriTemplate;
151+
public UriBuilderFactory getUriBuilderFactory() {
152+
return this.uriBuilderFactory;
147153
}
148154

149155
/**
150-
* Return the {@link UriBuilderFactory} to expand
151-
* the {@link HttpRequestValues#uriTemplate} with.
152-
* <p>This comes from a {@link UriBuilderFactory} method argument.
153-
* It allows you to override the {@code baseUri}, while keeping the
154-
* path as defined in class and method
155-
* {@code HttpExchange} annotations.
156+
* Return the URL template for the request. This comes from the values in
157+
* class and method {@code HttpExchange} annotations.
156158
*/
157159
@Nullable
158-
public UriBuilderFactory getUriBuilderFactory() {
159-
return this.uriBuilderFactory;
160+
public String getUriTemplate() {
161+
return this.uriTemplate;
160162
}
161163

162164
/**
@@ -237,10 +239,10 @@ public static class Builder {
237239
private URI uri;
238240

239241
@Nullable
240-
private String uriTemplate;
242+
private UriBuilderFactory uriBuilderFactory;
241243

242244
@Nullable
243-
private UriBuilderFactory uriBuilderFactory;
245+
private String uriTemplate;
244246

245247
@Nullable
246248
private Map<String, String> uriVars;
@@ -282,19 +284,20 @@ public Builder setUri(URI uri) {
282284
}
283285

284286
/**
285-
* Set the request URL as a String template.
287+
* Set the {@link UriBuilderFactory} that will be used to expand the
288+
* {@link #getUriTemplate()}.
289+
* @since 6.1
286290
*/
287-
public Builder setUriTemplate(String uriTemplate) {
288-
this.uriTemplate = uriTemplate;
291+
public Builder setUriBuilderFactory(@Nullable UriBuilderFactory uriBuilderFactory) {
292+
this.uriBuilderFactory = uriBuilderFactory;
289293
return this;
290294
}
291295

292296
/**
293-
* Set the {@link UriBuilderFactory} that
294-
* will be used to expand the URI.
297+
* Set the request URL as a String template.
295298
*/
296-
public Builder setUriBuilderFactory(@Nullable UriBuilderFactory uriBuilderFactory) {
297-
this.uriBuilderFactory = uriBuilderFactory;
299+
public Builder setUriTemplate(String uriTemplate) {
300+
this.uriTemplate = uriTemplate;
298301
return this;
299302
}
300303

@@ -427,8 +430,8 @@ public <T, P extends Publisher<T>> void setBody(P body, ParameterizedTypeReferen
427430
public HttpRequestValues build() {
428431

429432
URI uri = this.uri;
430-
String uriTemplate = (this.uriTemplate != null ? this.uriTemplate : "");
431433
UriBuilderFactory uriBuilderFactory = this.uriBuilderFactory;
434+
String uriTemplate = (this.uriTemplate != null ? this.uriTemplate : "");
432435
Map<String, String> uriVars = (this.uriVars != null ? new HashMap<>(this.uriVars) : Collections.emptyMap());
433436

434437
Object bodyValue = this.bodyValue;
@@ -470,8 +473,8 @@ else if (uri != null) {
470473
new HashMap<>(this.attributes) : Collections.emptyMap());
471474

472475
return createRequestValues(
473-
this.httpMethod, uri, uriTemplate, uriBuilderFactory,
474-
uriVars, headers, cookies, attributes, bodyValue);
476+
this.httpMethod, uri, uriBuilderFactory, uriTemplate, uriVars,
477+
headers, cookies, attributes, bodyValue);
475478
}
476479

477480
protected boolean hasParts() {
@@ -512,9 +515,9 @@ private String appendQueryParams(
512515

513516
/**
514517
* Create {@link HttpRequestValues} from values passed to the {@link Builder}.
515-
* @deprecated in favour of {@link Builder#createRequestValues(
516-
* HttpMethod, URI, String, UriBuilderFactory, Map, HttpHeaders,
517-
* MultiValueMap, Map, Object)} to be removed in 6.2.
518+
* @deprecated in favour of
519+
* {@link Builder#createRequestValues(HttpMethod, URI, UriBuilderFactory, String, Map, HttpHeaders, MultiValueMap, Map, Object)}
520+
* to be removed in 6.2.
518521
*/
519522
@Deprecated(since = "6.1", forRemoval = true)
520523
protected HttpRequestValues createRequestValues(
@@ -524,22 +527,23 @@ protected HttpRequestValues createRequestValues(
524527
HttpHeaders headers, MultiValueMap<String, String> cookies, Map<String, Object> attributes,
525528
@Nullable Object bodyValue) {
526529

527-
return createRequestValues(httpMethod, uri, uriTemplate, null,
530+
return createRequestValues(httpMethod, uri, null, uriTemplate,
528531
uriVars, headers, cookies, attributes, bodyValue);
529532
}
530533

531534
/**
532535
* Create {@link HttpRequestValues} from values passed to the {@link Builder}.
536+
* @since 6.1
533537
*/
534538
protected HttpRequestValues createRequestValues(
535539
@Nullable HttpMethod httpMethod,
536-
@Nullable URI uri, @Nullable String uriTemplate,
537-
@Nullable UriBuilderFactory uriBuilderFactory, Map<String, String> uriVars,
540+
@Nullable URI uri, @Nullable UriBuilderFactory uriBuilderFactory, @Nullable String uriTemplate,
541+
Map<String, String> uriVars,
538542
HttpHeaders headers, MultiValueMap<String, String> cookies, Map<String, Object> attributes,
539543
@Nullable Object bodyValue) {
540544

541545
return new HttpRequestValues(
542-
this.httpMethod, uri, uriTemplate, uriBuilderFactory,
546+
this.httpMethod, uri, uriBuilderFactory, uriTemplate,
543547
uriVars, headers, cookies, attributes, bodyValue);
544548
}
545549
}

spring-web/src/main/java/org/springframework/web/service/invoker/ReactiveHttpRequestValues.java

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,12 @@ public final class ReactiveHttpRequestValues extends HttpRequestValues {
5151

5252
private ReactiveHttpRequestValues(
5353
@Nullable HttpMethod httpMethod,
54-
@Nullable URI uri, @Nullable String uriTemplate,
55-
@Nullable UriBuilderFactory uriBuilderFactory, Map<String, String> uriVariables,
54+
@Nullable URI uri, @Nullable UriBuilderFactory uriBuilderFactory,
55+
@Nullable String uriTemplate, Map<String, String> uriVars,
5656
HttpHeaders headers, MultiValueMap<String, String> cookies, Map<String, Object> attributes,
5757
@Nullable Object bodyValue, @Nullable Publisher<?> body, @Nullable ParameterizedTypeReference<?> elementType) {
5858

59-
super(httpMethod, uri, uriTemplate, uriBuilderFactory,
60-
uriVariables, headers, cookies, attributes, bodyValue);
61-
59+
super(httpMethod, uri, uriBuilderFactory, uriTemplate, uriVars, headers, cookies, attributes, bodyValue);
6260
this.body = body;
6361
this.bodyElementType = elementType;
6462
}
@@ -135,14 +133,14 @@ public Builder setUri(URI uri) {
135133
}
136134

137135
@Override
138-
public Builder setUriTemplate(String uriTemplate) {
139-
super.setUriTemplate(uriTemplate);
136+
public Builder setUriBuilderFactory(@Nullable UriBuilderFactory uriBuilderFactory) {
137+
super.setUriBuilderFactory(uriBuilderFactory);
140138
return this;
141139
}
142140

143141
@Override
144-
public Builder setUriBuilderFactory(UriBuilderFactory uriBuilderFactory) {
145-
super.setUriBuilderFactory(uriBuilderFactory);
142+
public Builder setUriTemplate(String uriTemplate) {
143+
super.setUriTemplate(uriTemplate);
146144
return this;
147145
}
148146

@@ -271,17 +269,15 @@ protected Object buildMultipartBody() {
271269
@Override
272270
protected ReactiveHttpRequestValues createRequestValues(
273271
@Nullable HttpMethod httpMethod,
274-
@Nullable URI uri, @Nullable String uriTemplate,
275-
@Nullable UriBuilderFactory uriBuilderFactory, Map<String, String> uriVars,
272+
@Nullable URI uri, @Nullable UriBuilderFactory uriBuilderFactory,
273+
@Nullable String uriTemplate, Map<String, String> uriVars,
276274
HttpHeaders headers, MultiValueMap<String, String> cookies, Map<String, Object> attributes,
277275
@Nullable Object bodyValue) {
278276

279277
return new ReactiveHttpRequestValues(
280-
httpMethod, uri, uriTemplate, uriBuilderFactory,
281-
uriVars, headers, cookies, attributes,
282-
bodyValue, this.body, this.bodyElementType);
278+
httpMethod, uri, uriBuilderFactory, uriTemplate, uriVars,
279+
headers, cookies, attributes, bodyValue, this.body, this.bodyElementType);
283280
}
284-
285281
}
286282

287283
}

0 commit comments

Comments
 (0)