Skip to content

Commit 57ed5bf

Browse files
committed
Polishing contribution
Closes gh-30869
1 parent 8b77ed1 commit 57ed5bf

File tree

5 files changed

+134
-339
lines changed

5 files changed

+134
-339
lines changed

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

+29-27
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,26 @@
3131
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
3232

3333
/**
34-
* {@link HttpExchangeAdapter} that enables an {@link HttpServiceProxyFactory} to use
35-
* {@link RestClient} for request execution.
34+
* {@link HttpExchangeAdapter} that enables an {@link HttpServiceProxyFactory}
35+
* to use {@link RestClient} for request execution.
3636
*
37-
* <p>
38-
* Use static factory methods in this class to create an {@link HttpServiceProxyFactory}
39-
* configured with a given {@link RestClient}.
37+
* <p>Use static factory methods in this class to create an
38+
* {@link HttpServiceProxyFactory} configured with the given {@link RestClient}.
4039
*
4140
* @author Olga Maciaszek-Sharma
41+
* @author Rossen Stoyanchev
4242
* @since 6.1
4343
*/
4444
public final class RestClientAdapter implements HttpExchangeAdapter {
4545

4646
private final RestClient restClient;
4747

48+
4849
private RestClientAdapter(RestClient restClient) {
4950
this.restClient = restClient;
5051
}
5152

53+
5254
@Override
5355
public boolean supportsRequestAttributes() {
5456
return true;
@@ -60,66 +62,66 @@ public void exchange(HttpRequestValues requestValues) {
6062
}
6163

6264
@Override
63-
public HttpHeaders exchangeForHeaders(HttpRequestValues requestValues) {
64-
return newRequest(requestValues).retrieve().toBodilessEntity().getHeaders();
65+
public HttpHeaders exchangeForHeaders(HttpRequestValues values) {
66+
return newRequest(values).retrieve().toBodilessEntity().getHeaders();
6567
}
6668

6769
@Override
68-
public <T> T exchangeForBody(HttpRequestValues requestValues, ParameterizedTypeReference<T> bodyType) {
69-
return newRequest(requestValues).retrieve().body(bodyType);
70+
public <T> T exchangeForBody(HttpRequestValues values, ParameterizedTypeReference<T> bodyType) {
71+
return newRequest(values).retrieve().body(bodyType);
7072
}
7173

7274
@Override
73-
public ResponseEntity<Void> exchangeForBodilessEntity(HttpRequestValues requestValues) {
74-
return newRequest(requestValues).retrieve().toBodilessEntity();
75+
public ResponseEntity<Void> exchangeForBodilessEntity(HttpRequestValues values) {
76+
return newRequest(values).retrieve().toBodilessEntity();
7577
}
7678

7779
@Override
78-
public <T> ResponseEntity<T> exchangeForEntity(HttpRequestValues requestValues,
79-
ParameterizedTypeReference<T> bodyType) {
80-
return newRequest(requestValues).retrieve().toEntity(bodyType);
80+
public <T> ResponseEntity<T> exchangeForEntity(HttpRequestValues values, ParameterizedTypeReference<T> bodyType) {
81+
return newRequest(values).retrieve().toEntity(bodyType);
8182
}
8283

83-
private RestClient.RequestBodySpec newRequest(HttpRequestValues requestValues) {
84+
private RestClient.RequestBodySpec newRequest(HttpRequestValues values) {
8485

85-
HttpMethod httpMethod = requestValues.getHttpMethod();
86+
HttpMethod httpMethod = values.getHttpMethod();
8687
Assert.notNull(httpMethod, "HttpMethod is required");
8788

8889
RestClient.RequestBodyUriSpec uriSpec = this.restClient.method(httpMethod);
8990

9091
RestClient.RequestBodySpec bodySpec;
91-
if (requestValues.getUri() != null) {
92-
bodySpec = uriSpec.uri(requestValues.getUri());
92+
if (values.getUri() != null) {
93+
bodySpec = uriSpec.uri(values.getUri());
9394
}
94-
else if (requestValues.getUriTemplate() != null) {
95-
bodySpec = uriSpec.uri(requestValues.getUriTemplate(), requestValues.getUriVariables());
95+
else if (values.getUriTemplate() != null) {
96+
bodySpec = uriSpec.uri(values.getUriTemplate(), values.getUriVariables());
9697
}
9798
else {
9899
throw new IllegalStateException("Neither full URL nor URI template");
99100
}
100101

101-
bodySpec.headers(headers -> headers.putAll(requestValues.getHeaders()));
102+
bodySpec.headers(headers -> headers.putAll(values.getHeaders()));
102103

103-
if (!requestValues.getCookies().isEmpty()) {
104+
if (!values.getCookies().isEmpty()) {
104105
List<String> cookies = new ArrayList<>();
105-
requestValues.getCookies().forEach((name, values) -> values.forEach(value -> {
106+
values.getCookies().forEach((name, cookieValues) -> cookieValues.forEach(value -> {
106107
HttpCookie cookie = new HttpCookie(name, value);
107108
cookies.add(cookie.toString());
108109
}));
109110
bodySpec.header(HttpHeaders.COOKIE, String.join("; ", cookies));
110111
}
111112

112-
bodySpec.attributes(attributes -> attributes.putAll(requestValues.getAttributes()));
113+
bodySpec.attributes(attributes -> attributes.putAll(values.getAttributes()));
113114

114-
if (requestValues.getBodyValue() != null) {
115-
bodySpec.body(requestValues.getBodyValue());
115+
if (values.getBodyValue() != null) {
116+
bodySpec.body(values.getBodyValue());
116117
}
117118

118119
return bodySpec;
119120
}
120121

122+
121123
/**
122-
* Create a {@link RestClientAdapter} with the given {@link RestClient}.
124+
* Create a {@link RestClientAdapter} for the given {@link RestClient}.
123125
*/
124126
public static RestClientAdapter create(RestClient restClient) {
125127
return new RestClientAdapter(restClient);

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

+26-28
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@
3434
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
3535

3636
/**
37-
* {@link HttpExchangeAdapter} that enables an {@link HttpServiceProxyFactory} to use
38-
* {@link RestTemplate} for request execution.
37+
* {@link HttpExchangeAdapter} that enables an {@link HttpServiceProxyFactory}
38+
* to use {@link RestTemplate} for request execution.
3939
*
4040
* <p>Use static factory methods in this class to create an
41-
* {@link HttpServiceProxyFactory} configured with a given {@link RestTemplate}.
41+
* {@link HttpServiceProxyFactory} configured with the given {@link RestTemplate}.
4242
*
4343
* @author Olga Maciaszek-Sharma
4444
* @since 6.1
@@ -59,71 +59,69 @@ public boolean supportsRequestAttributes() {
5959
}
6060

6161
@Override
62-
public void exchange(HttpRequestValues requestValues) {
63-
this.restTemplate.exchange(newRequest(requestValues), Void.class);
62+
public void exchange(HttpRequestValues values) {
63+
this.restTemplate.exchange(newRequest(values), Void.class);
6464
}
6565

6666
@Override
67-
public HttpHeaders exchangeForHeaders(HttpRequestValues requestValues) {
68-
return this.restTemplate.exchange(newRequest(requestValues), Void.class).getHeaders();
67+
public HttpHeaders exchangeForHeaders(HttpRequestValues values) {
68+
return this.restTemplate.exchange(newRequest(values), Void.class).getHeaders();
6969
}
7070

7171
@Override
72-
public <T> T exchangeForBody(HttpRequestValues requestValues, ParameterizedTypeReference<T> bodyType) {
73-
return this.restTemplate.exchange(newRequest(requestValues), bodyType).getBody();
72+
public <T> T exchangeForBody(HttpRequestValues values, ParameterizedTypeReference<T> bodyType) {
73+
return this.restTemplate.exchange(newRequest(values), bodyType).getBody();
7474
}
7575

7676
@Override
77-
public ResponseEntity<Void> exchangeForBodilessEntity(HttpRequestValues requestValues) {
78-
return this.restTemplate.exchange(newRequest(requestValues), Void.class);
77+
public ResponseEntity<Void> exchangeForBodilessEntity(HttpRequestValues values) {
78+
return this.restTemplate.exchange(newRequest(values), Void.class);
7979
}
8080

8181
@Override
82-
public <T> ResponseEntity<T> exchangeForEntity(
83-
HttpRequestValues requestValues, ParameterizedTypeReference<T> bodyType) {
84-
85-
return this.restTemplate.exchange(newRequest(requestValues), bodyType);
82+
public <T> ResponseEntity<T> exchangeForEntity(HttpRequestValues values, ParameterizedTypeReference<T> bodyType) {
83+
return this.restTemplate.exchange(newRequest(values), bodyType);
8684
}
8785

88-
private RequestEntity<?> newRequest(HttpRequestValues requestValues) {
86+
private RequestEntity<?> newRequest(HttpRequestValues values) {
8987
URI uri;
90-
if (requestValues.getUri() != null) {
91-
uri = requestValues.getUri();
88+
if (values.getUri() != null) {
89+
uri = values.getUri();
9290
}
93-
else if (requestValues.getUriTemplate() != null) {
94-
String uriTemplate = requestValues.getUriTemplate();
95-
Map<String, String> variables = requestValues.getUriVariables();
91+
else if (values.getUriTemplate() != null) {
92+
String uriTemplate = values.getUriTemplate();
93+
Map<String, String> variables = values.getUriVariables();
9694
uri = this.restTemplate.getUriTemplateHandler().expand(uriTemplate, variables);
9795
}
9896
else {
9997
throw new IllegalStateException("Neither full URL nor URI template");
10098
}
10199

102-
HttpMethod httpMethod = requestValues.getHttpMethod();
100+
HttpMethod httpMethod = values.getHttpMethod();
103101
Assert.notNull(httpMethod, "HttpMethod is required");
104102

105103
RequestEntity.BodyBuilder builder = RequestEntity.method(httpMethod, uri);
106-
builder.headers(requestValues.getHeaders());
104+
builder.headers(values.getHeaders());
107105

108-
if (!requestValues.getCookies().isEmpty()) {
106+
if (!values.getCookies().isEmpty()) {
109107
List<String> cookies = new ArrayList<>();
110-
requestValues.getCookies().forEach((name, values) -> values.forEach(value -> {
108+
values.getCookies().forEach((name, cookieValues) -> cookieValues.forEach(value -> {
111109
HttpCookie cookie = new HttpCookie(name, value);
112110
cookies.add(cookie.toString());
113111
}));
114112
builder.header(HttpHeaders.COOKIE, String.join("; ", cookies));
115113
}
116114

117-
if (requestValues.getBodyValue() != null) {
118-
return builder.body(requestValues.getBodyValue());
115+
if (values.getBodyValue() != null) {
116+
return builder.body(values.getBodyValue());
119117
}
120118

121119
return builder.build();
122120
}
123121

124122

125123
/**
126-
* Create a {@link RestTemplateAdapter} with the given {@link RestTemplate}.
124+
* Create a {@link RestTemplateAdapter} for the given {@link RestTemplate}.
127125
*/
128126
public static RestTemplateAdapter create(RestTemplate restTemplate) {
129127
return new RestTemplateAdapter(restTemplate);

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@
5353
*
5454
* @author Rossen Stoyanchev
5555
* @since 6.0
56-
* @see org.springframework.web.client.support.RestTemplateAdapter
57-
* @see org.springframework.web.reactive.function.client.support.WebClientAdapter
5856
* @see org.springframework.web.client.support.RestClientAdapter
57+
* @see org.springframework.web.reactive.function.client.support.WebClientAdapter
58+
* @see org.springframework.web.client.support.RestTemplateAdapter
5959
*/
6060
public final class HttpServiceProxyFactory {
6161

0 commit comments

Comments
 (0)