Fix missing URI template from client observations in ResTemplateAdapter

Prior to this commit, the `RestTemplateAdapter` would manually expand
templated URIs. This means that the `RestTemplate` instance itself would
never see the templated URIs and could not record it in the client
observations at runtime.

This commit ensures that when URI templates are available, the adapter
uses the correct `exchange` method variant.

Fixes gh-31144
This commit is contained in:
Brian Clozel
2023-09-04 11:45:35 +02:00
parent e8fd29091c
commit 5fb567a37e
2 changed files with 30 additions and 21 deletions

View File

@@ -16,10 +16,8 @@
package org.springframework.web.client.support;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpCookie;
@@ -41,6 +39,7 @@ import org.springframework.web.service.invoker.HttpServiceProxyFactory;
* {@link HttpServiceProxyFactory} configured with the given {@link RestTemplate}.
*
* @author Olga Maciaszek-Sharma
* @author Brian Clozel
* @since 6.1
*/
public final class RestTemplateAdapter implements HttpExchangeAdapter {
@@ -84,23 +83,21 @@ public final class RestTemplateAdapter implements HttpExchangeAdapter {
}
private RequestEntity<?> newRequest(HttpRequestValues values) {
URI uri;
HttpMethod httpMethod = values.getHttpMethod();
Assert.notNull(httpMethod, "HttpMethod is required");
RequestEntity.BodyBuilder builder;
if (values.getUri() != null) {
uri = values.getUri();
builder = RequestEntity.method(httpMethod, values.getUri());
}
else if (values.getUriTemplate() != null) {
String uriTemplate = values.getUriTemplate();
Map<String, String> variables = values.getUriVariables();
uri = this.restTemplate.getUriTemplateHandler().expand(uriTemplate, variables);
builder = RequestEntity.method(httpMethod, values.getUriTemplate(), values.getUriVariables());
}
else {
throw new IllegalStateException("Neither full URL nor URI template");
}
HttpMethod httpMethod = values.getHttpMethod();
Assert.notNull(httpMethod, "HttpMethod is required");
RequestEntity.BodyBuilder builder = RequestEntity.method(httpMethod, uri);
builder.headers(values.getHeaders());
if (!values.getCookies().isEmpty()) {