Skip to content

Commit 3b4d416

Browse files
committed
GH-10830: Refine HTTP RestClient delegation and local client lifecycle
Fixes gh-10830 * refactor nullable `RestClient` HTTP DSL factory methods to delegate via RestClient-first paths and remove duplicate fallback branches * add shared `outboundGatewaySpec(...)` helpers in `Http` to centralize URI/String/Expression client selection * add non-deprecated local constructors in `HttpMessageHandlerSpec` for URI/String/Expression paths * keep deprecated RestTemplate DSL overloads and add `@SuppressWarnings("removal")` where they intentionally call deprecated constructors * build local `RestClient` once in `HttpRequestExecutingMessageHandler#doInit()` and stop rebuilding on each local mutator call * keep local mutators (`setRequestFactory`, `setErrorHandler`, `setMessageConverters`) applying to local `RestClient.Builder` * align HTTP proxy test flow with lifecycle semantics by re-initializing after mutator-based setup * revise HTTP proxy/cookie test fixtures and formatting updates for the current RestClient-backed local behavior * add `[[x7.1-http-changes]]` section to `whats-new.adoc` documenting HTTP 7.1 updates Signed-off-by: Arun Sethumadhavan <mailaruns@gmail.com>
1 parent 223c120 commit 3b4d416

6 files changed

Lines changed: 62 additions & 45 deletions

File tree

spring-integration-http/src/main/java/org/springframework/integration/http/dsl/Http.java

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ public static HttpMessageHandlerSpec outboundChannelAdapter(Expression uriExpres
9090
* @deprecated Since 7.1 in favor of {@link RestClient}-based configuration.
9191
*/
9292
@Deprecated(since = "7.1", forRemoval = true)
93+
@SuppressWarnings("removal")
9394
public static HttpMessageHandlerSpec outboundChannelAdapter(URI uri, @Nullable RestTemplate restTemplate) {
9495
return new HttpMessageHandlerSpec(uri, restTemplate).expectReply(false);
9596
}
@@ -103,9 +104,7 @@ public static HttpMessageHandlerSpec outboundChannelAdapter(URI uri, @Nullable R
103104
* @since 7.1
104105
*/
105106
public static HttpMessageHandlerSpec outboundChannelAdapter(URI uri, @Nullable RestClient restClient) {
106-
return (restClient != null
107-
? new HttpMessageHandlerSpec(uri, restClient).expectReply(false)
108-
: outboundChannelAdapter(uri, (RestTemplate) null));
107+
return outboundGatewaySpec(uri, restClient).expectReply(false);
109108
}
110109

111110
/**
@@ -117,6 +116,7 @@ public static HttpMessageHandlerSpec outboundChannelAdapter(URI uri, @Nullable R
117116
* @deprecated Since 7.1 in favor of {@link RestClient}-based configuration.
118117
*/
119118
@Deprecated(since = "7.1", forRemoval = true)
119+
@SuppressWarnings("removal")
120120
public static HttpMessageHandlerSpec outboundChannelAdapter(String uri, @Nullable RestTemplate restTemplate) {
121121
return new HttpMessageHandlerSpec(uri, restTemplate).expectReply(false);
122122
}
@@ -130,9 +130,7 @@ public static HttpMessageHandlerSpec outboundChannelAdapter(String uri, @Nullabl
130130
* @since 7.1
131131
*/
132132
public static HttpMessageHandlerSpec outboundChannelAdapter(String uri, @Nullable RestClient restClient) {
133-
return (restClient != null
134-
? new HttpMessageHandlerSpec(uri, restClient).expectReply(false)
135-
: outboundChannelAdapter(uri, (RestTemplate) null));
133+
return outboundGatewaySpec(uri, restClient).expectReply(false);
136134
}
137135

138136
/**
@@ -178,6 +176,7 @@ public static <P> HttpMessageHandlerSpec outboundChannelAdapter(Function<Message
178176
* @deprecated Since 7.1 in favor of {@link RestClient}-based configuration.
179177
*/
180178
@Deprecated(since = "7.1", forRemoval = true)
179+
@SuppressWarnings("removal")
181180
public static HttpMessageHandlerSpec outboundChannelAdapter(Expression uriExpression,
182181
@Nullable RestTemplate restTemplate) {
183182

@@ -194,9 +193,7 @@ public static HttpMessageHandlerSpec outboundChannelAdapter(Expression uriExpres
194193
* @since 7.1
195194
*/
196195
public static HttpMessageHandlerSpec outboundChannelAdapter(Expression uriExpression, @Nullable RestClient restClient) {
197-
return (restClient != null
198-
? new HttpMessageHandlerSpec(uriExpression, restClient).expectReply(false)
199-
: outboundChannelAdapter(uriExpression, (RestTemplate) null));
196+
return outboundGatewaySpec(uriExpression, restClient).expectReply(false);
200197
}
201198

202199
/**
@@ -247,6 +244,7 @@ public static HttpMessageHandlerSpec outboundGateway(Expression uriExpression) {
247244
* @deprecated Since 7.1 in favor of {@link RestClient}-based configuration.
248245
*/
249246
@Deprecated(since = "7.1", forRemoval = true)
247+
@SuppressWarnings("removal")
250248
public static HttpMessageHandlerSpec outboundGateway(URI uri, @Nullable RestTemplate restTemplate) {
251249
return new HttpMessageHandlerSpec(uri, restTemplate);
252250
}
@@ -260,9 +258,7 @@ public static HttpMessageHandlerSpec outboundGateway(URI uri, @Nullable RestTemp
260258
* @since 7.1
261259
*/
262260
public static HttpMessageHandlerSpec outboundGateway(URI uri, @Nullable RestClient restClient) {
263-
return (restClient != null
264-
? new HttpMessageHandlerSpec(uri, restClient)
265-
: outboundGateway(uri, (RestTemplate) null));
261+
return outboundGatewaySpec(uri, restClient);
266262
}
267263

268264
/**
@@ -274,6 +270,7 @@ public static HttpMessageHandlerSpec outboundGateway(URI uri, @Nullable RestClie
274270
* @deprecated Since 7.1 in favor of {@link RestClient}-based configuration.
275271
*/
276272
@Deprecated(since = "7.1", forRemoval = true)
273+
@SuppressWarnings("removal")
277274
public static HttpMessageHandlerSpec outboundGateway(String uri, @Nullable RestTemplate restTemplate) {
278275
return new HttpMessageHandlerSpec(uri, restTemplate);
279276
}
@@ -287,9 +284,7 @@ public static HttpMessageHandlerSpec outboundGateway(String uri, @Nullable RestT
287284
* @since 7.1
288285
*/
289286
public static HttpMessageHandlerSpec outboundGateway(String uri, @Nullable RestClient restClient) {
290-
return (restClient != null
291-
? new HttpMessageHandlerSpec(uri, restClient)
292-
: outboundGateway(uri, (RestTemplate) null));
287+
return outboundGatewaySpec(uri, restClient);
293288
}
294289

295290
/**
@@ -335,6 +330,7 @@ public static <P> HttpMessageHandlerSpec outboundGateway(Function<Message<P>, ?>
335330
* @deprecated Since 7.1 in favor of {@link RestClient}-based configuration.
336331
*/
337332
@Deprecated(since = "7.1", forRemoval = true)
333+
@SuppressWarnings("removal")
338334
public static HttpMessageHandlerSpec outboundGateway(Expression uriExpression,
339335
@Nullable RestTemplate restTemplate) {
340336

@@ -351,9 +347,23 @@ public static HttpMessageHandlerSpec outboundGateway(Expression uriExpression,
351347
* @since 7.1
352348
*/
353349
public static HttpMessageHandlerSpec outboundGateway(Expression uriExpression, @Nullable RestClient restClient) {
354-
return (restClient != null
350+
return outboundGatewaySpec(uriExpression, restClient);
351+
}
352+
353+
private static HttpMessageHandlerSpec outboundGatewaySpec(URI uri, @Nullable RestClient restClient) {
354+
return restClient != null ? new HttpMessageHandlerSpec(uri, restClient) : new HttpMessageHandlerSpec(uri);
355+
}
356+
357+
private static HttpMessageHandlerSpec outboundGatewaySpec(String uri, @Nullable RestClient restClient) {
358+
return restClient != null ? new HttpMessageHandlerSpec(uri, restClient) : new HttpMessageHandlerSpec(uri);
359+
}
360+
361+
private static HttpMessageHandlerSpec outboundGatewaySpec(Expression uriExpression,
362+
@Nullable RestClient restClient) {
363+
364+
return restClient != null
355365
? new HttpMessageHandlerSpec(uriExpression, restClient)
356-
: outboundGateway(uriExpression, (RestTemplate) null));
366+
: new HttpMessageHandlerSpec(uriExpression);
357367
}
358368

359369
/**

spring-integration-http/src/main/java/org/springframework/integration/http/dsl/HttpMessageHandlerSpec.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,19 @@ public class HttpMessageHandlerSpec
4949

5050
private final boolean clientSet;
5151

52+
protected HttpMessageHandlerSpec(URI uri) {
53+
this(new ValueExpression<>(uri));
54+
}
55+
56+
protected HttpMessageHandlerSpec(String uri) {
57+
this(new LiteralExpression(uri));
58+
}
59+
60+
protected HttpMessageHandlerSpec(Expression uriExpression) {
61+
super(new HttpRequestExecutingMessageHandler(uriExpression));
62+
this.clientSet = false;
63+
}
64+
5265
/**
5366
* @deprecated Since 7.1 in favor of {@link RestClient}-based configuration.
5467
*/
@@ -72,7 +85,7 @@ protected HttpMessageHandlerSpec(String uri, @Nullable RestTemplate restTemplate
7285
protected HttpMessageHandlerSpec(Expression uriExpression, @Nullable RestTemplate restTemplate) {
7386
super(restTemplate != null
7487
? new HttpRequestExecutingMessageHandler(uriExpression, RestClient.create(restTemplate))
75-
: new HttpRequestExecutingMessageHandler(uriExpression, (RestTemplate) null));
88+
: new HttpRequestExecutingMessageHandler(uriExpression));
7689
this.clientSet = restTemplate != null;
7790
}
7891

spring-integration-http/src/main/java/org/springframework/integration/http/outbound/HttpRequestExecutingMessageHandler.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,6 @@ private void assertLocalClient(String option) {
200200
@Override
201201
protected void doInit() {
202202
super.doInit();
203-
rebuildLocalRestClient();
204-
}
205-
206-
private void rebuildLocalRestClient() {
207203
RestClient.Builder localRestClientBuilder = this.localRestClientBuilder;
208204
if (localRestClientBuilder != null) {
209205
this.restClient = localRestClientBuilder.build();
@@ -220,7 +216,6 @@ public void setErrorHandler(ResponseErrorHandler errorHandler) {
220216
RestClient.Builder localRestClientBuilder = this.localRestClientBuilder;
221217
Assert.state(localRestClientBuilder != null, "'localRestClientBuilder' must not be null");
222218
localRestClientBuilder.defaultStatusHandler(errorHandler);
223-
rebuildLocalRestClient();
224219
}
225220

226221
/**
@@ -229,13 +224,12 @@ public void setErrorHandler(ResponseErrorHandler errorHandler) {
229224
* @param messageConverters The message converters.
230225
* @see RestTemplate#setMessageConverters(java.util.List)
231226
*/
227+
@SuppressWarnings("removal")
232228
public void setMessageConverters(List<HttpMessageConverter<?>> messageConverters) {
233229
assertLocalClient("messageConverters");
234230
RestClient.Builder localRestClientBuilder = this.localRestClientBuilder;
235231
Assert.state(localRestClientBuilder != null, "'localRestClientBuilder' must not be null");
236-
@SuppressWarnings("removal")
237-
RestClient.Builder builder = localRestClientBuilder.messageConverters(messageConverters);
238-
this.restClient = builder.build();
232+
localRestClientBuilder.messageConverters(messageConverters);
239233
}
240234

241235
/**
@@ -248,7 +242,6 @@ public void setRequestFactory(ClientHttpRequestFactory requestFactory) {
248242
RestClient.Builder localRestClientBuilder = this.localRestClientBuilder;
249243
Assert.state(localRestClientBuilder != null, "'localRestClientBuilder' must not be null");
250244
localRestClientBuilder.requestFactory(requestFactory);
251-
rebuildLocalRestClient();
252245
}
253246

254247
@Override

spring-integration-http/src/test/java/org/springframework/integration/http/HttpProxyScenarioTests.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,10 @@ public void testHttpProxyScenario() throws Exception {
136136
.exchange(Mockito.anyString(), Mockito.any(HttpMethod.class),
137137
Mockito.any(HttpEntity.class), Mockito.<Class<?>>any(), Mockito.anyMap());
138138

139-
PropertyAccessor dfa = new DirectFieldAccessor(this.handler);
140-
dfa.setPropertyValue("localRestClientBuilder", null);
141-
dfa.setPropertyValue("restClient", null);
142-
dfa.setPropertyValue("restTemplate", template);
139+
PropertyAccessor dfa = new DirectFieldAccessor(this.handler);
140+
dfa.setPropertyValue("localRestClientBuilder", null);
141+
dfa.setPropertyValue("restClient", null);
142+
dfa.setPropertyValue("restTemplate", template);
143143

144144
RequestAttributes attributes = new ServletRequestAttributes(request);
145145
RequestContextHolder.setRequestAttributes(attributes);
@@ -199,10 +199,10 @@ public void testHttpMultipartProxyScenario() throws Exception {
199199
.exchange(Mockito.anyString(), Mockito.any(HttpMethod.class),
200200
Mockito.any(HttpEntity.class), Mockito.<Class<?>>any(), Mockito.anyMap());
201201

202-
PropertyAccessor dfa = new DirectFieldAccessor(this.handlermp);
203-
dfa.setPropertyValue("localRestClientBuilder", null);
204-
dfa.setPropertyValue("restClient", null);
205-
dfa.setPropertyValue("restTemplate", template);
202+
PropertyAccessor dfa = new DirectFieldAccessor(this.handlermp);
203+
dfa.setPropertyValue("localRestClientBuilder", null);
204+
dfa.setPropertyValue("restClient", null);
205+
dfa.setPropertyValue("restTemplate", template);
206206

207207
RequestAttributes attributes = new ServletRequestAttributes(request);
208208
RequestContextHolder.setRequestAttributes(attributes);

spring-integration-http/src/test/java/org/springframework/integration/http/dsl/HttpDslTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ public void testHttpProxyFlow() throws Exception {
131131
ClientHttpRequestFactory mockRequestFactory =
132132
TestUtils.getPropertyValue(restTestClient, "restClient.clientRequestFactory");
133133
this.serviceInternalGatewayHandler.setRequestFactory(mockRequestFactory);
134+
this.serviceInternalGatewayHandler.afterPropertiesSet();
134135

135136
this.mockMvc.perform(
136137
get("/service")

spring-integration-http/src/test/java/org/springframework/integration/http/outbound/CookieTests.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,15 @@ public static class RequestFactory implements ClientHttpRequestFactory {
9292
private int count = 123;
9393

9494
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) {
95-
return new ClientHttpRequest() {
95+
return new ClientHttpRequest() {
9696

97-
private final HttpHeaders headers = new HttpHeaders();
97+
private final HttpHeaders headers = new HttpHeaders();
9898

99-
private final Map<String, Object> attributes = new HashMap<>();
99+
private final Map<String, Object> attributes = new HashMap<>();
100100

101-
public HttpHeaders getHeaders() {
102-
return headers;
103-
}
101+
public HttpHeaders getHeaders() {
102+
return headers;
103+
}
104104

105105
public OutputStream getBody() {
106106
return bos;
@@ -114,10 +114,10 @@ public HttpMethod getMethod() {
114114
return null;
115115
}
116116

117-
@Override
118-
public Map<String, Object> getAttributes() {
119-
return this.attributes;
120-
}
117+
@Override
118+
public Map<String, Object> getAttributes() {
119+
return this.attributes;
120+
}
121121

122122
public ClientHttpResponse execute() {
123123
allHeaders.add(headers);

0 commit comments

Comments
 (0)