Skip to content

Commit 84f2632

Browse files
committed
GH-10830: Refine HTTP RestClient delegation
Fixes: gh-10830 * route deprecated RestTemplate HTTP DSL/spec paths through RestClient.create(RestTemplate) * remove deprecated internal-call suppressions in Http factory methods and simplify nullable RestClient delegation * align HttpRequestExecutingMessageHandler default constructor flow with RestClient-first initialization * switch local message converter setup to non-deprecated RestClient.Builder.configureMessageConverters API * update HTTP parser/DSL tests and reduce deprecated-constructor warnings in outbound handler tests * What's New entry for HTTP RestClient/RestTemplate migration notes Signed-off-by: Arun Sethumadhavan <mailaruns@gmail.com>
1 parent 3b4d416 commit 84f2632

7 files changed

Lines changed: 98 additions & 106 deletions

File tree

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

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,8 @@ 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")
9493
public static HttpMessageHandlerSpec outboundChannelAdapter(URI uri, @Nullable RestTemplate restTemplate) {
95-
return new HttpMessageHandlerSpec(uri, restTemplate).expectReply(false);
94+
return outboundChannelAdapter(uri, toRestClient(restTemplate));
9695
}
9796

9897
/**
@@ -116,9 +115,8 @@ public static HttpMessageHandlerSpec outboundChannelAdapter(URI uri, @Nullable R
116115
* @deprecated Since 7.1 in favor of {@link RestClient}-based configuration.
117116
*/
118117
@Deprecated(since = "7.1", forRemoval = true)
119-
@SuppressWarnings("removal")
120118
public static HttpMessageHandlerSpec outboundChannelAdapter(String uri, @Nullable RestTemplate restTemplate) {
121-
return new HttpMessageHandlerSpec(uri, restTemplate).expectReply(false);
119+
return outboundChannelAdapter(uri, toRestClient(restTemplate));
122120
}
123121

124122
/**
@@ -147,7 +145,7 @@ public static HttpMessageHandlerSpec outboundChannelAdapter(String uri, @Nullabl
147145
public static <P> HttpMessageHandlerSpec outboundChannelAdapter(Function<Message<P>, ?> uriFunction,
148146
RestTemplate restTemplate) {
149147

150-
return outboundChannelAdapter(new FunctionExpression<>(uriFunction), restTemplate);
148+
return outboundChannelAdapter(new FunctionExpression<>(uriFunction), toRestClient(restTemplate));
151149
}
152150

153151
/**
@@ -176,11 +174,10 @@ public static <P> HttpMessageHandlerSpec outboundChannelAdapter(Function<Message
176174
* @deprecated Since 7.1 in favor of {@link RestClient}-based configuration.
177175
*/
178176
@Deprecated(since = "7.1", forRemoval = true)
179-
@SuppressWarnings("removal")
180177
public static HttpMessageHandlerSpec outboundChannelAdapter(Expression uriExpression,
181178
@Nullable RestTemplate restTemplate) {
182179

183-
return new HttpMessageHandlerSpec(uriExpression, restTemplate).expectReply(false);
180+
return outboundChannelAdapter(uriExpression, toRestClient(restTemplate));
184181
}
185182

186183
/**
@@ -244,9 +241,8 @@ public static HttpMessageHandlerSpec outboundGateway(Expression uriExpression) {
244241
* @deprecated Since 7.1 in favor of {@link RestClient}-based configuration.
245242
*/
246243
@Deprecated(since = "7.1", forRemoval = true)
247-
@SuppressWarnings("removal")
248244
public static HttpMessageHandlerSpec outboundGateway(URI uri, @Nullable RestTemplate restTemplate) {
249-
return new HttpMessageHandlerSpec(uri, restTemplate);
245+
return outboundGateway(uri, toRestClient(restTemplate));
250246
}
251247

252248
/**
@@ -270,9 +266,8 @@ public static HttpMessageHandlerSpec outboundGateway(URI uri, @Nullable RestClie
270266
* @deprecated Since 7.1 in favor of {@link RestClient}-based configuration.
271267
*/
272268
@Deprecated(since = "7.1", forRemoval = true)
273-
@SuppressWarnings("removal")
274269
public static HttpMessageHandlerSpec outboundGateway(String uri, @Nullable RestTemplate restTemplate) {
275-
return new HttpMessageHandlerSpec(uri, restTemplate);
270+
return outboundGateway(uri, toRestClient(restTemplate));
276271
}
277272

278273
/**
@@ -301,7 +296,7 @@ public static HttpMessageHandlerSpec outboundGateway(String uri, @Nullable RestC
301296
public static <P> HttpMessageHandlerSpec outboundGateway(Function<Message<P>, ?> uriFunction,
302297
RestTemplate restTemplate) {
303298

304-
return outboundGateway(new FunctionExpression<>(uriFunction), restTemplate);
299+
return outboundGateway(new FunctionExpression<>(uriFunction), toRestClient(restTemplate));
305300
}
306301

307302
/**
@@ -330,11 +325,10 @@ public static <P> HttpMessageHandlerSpec outboundGateway(Function<Message<P>, ?>
330325
* @deprecated Since 7.1 in favor of {@link RestClient}-based configuration.
331326
*/
332327
@Deprecated(since = "7.1", forRemoval = true)
333-
@SuppressWarnings("removal")
334328
public static HttpMessageHandlerSpec outboundGateway(Expression uriExpression,
335329
@Nullable RestTemplate restTemplate) {
336330

337-
return new HttpMessageHandlerSpec(uriExpression, restTemplate);
331+
return outboundGateway(uriExpression, toRestClient(restTemplate));
338332
}
339333

340334
/**
@@ -351,19 +345,21 @@ public static HttpMessageHandlerSpec outboundGateway(Expression uriExpression, @
351345
}
352346

353347
private static HttpMessageHandlerSpec outboundGatewaySpec(URI uri, @Nullable RestClient restClient) {
354-
return restClient != null ? new HttpMessageHandlerSpec(uri, restClient) : new HttpMessageHandlerSpec(uri);
348+
return new HttpMessageHandlerSpec(uri, restClient);
355349
}
356350

357351
private static HttpMessageHandlerSpec outboundGatewaySpec(String uri, @Nullable RestClient restClient) {
358-
return restClient != null ? new HttpMessageHandlerSpec(uri, restClient) : new HttpMessageHandlerSpec(uri);
352+
return new HttpMessageHandlerSpec(uri, restClient);
359353
}
360354

361355
private static HttpMessageHandlerSpec outboundGatewaySpec(Expression uriExpression,
362356
@Nullable RestClient restClient) {
363357

364-
return restClient != null
365-
? new HttpMessageHandlerSpec(uriExpression, restClient)
366-
: new HttpMessageHandlerSpec(uriExpression);
358+
return new HttpMessageHandlerSpec(uriExpression, restClient);
359+
}
360+
361+
private static @Nullable RestClient toRestClient(@Nullable RestTemplate restTemplate) {
362+
return restTemplate != null ? RestClient.create(restTemplate) : null;
367363
}
368364

369365
/**

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

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -49,57 +49,41 @@ 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-
6552
/**
6653
* @deprecated Since 7.1 in favor of {@link RestClient}-based configuration.
6754
*/
6855
@Deprecated(since = "7.1", forRemoval = true)
6956
protected HttpMessageHandlerSpec(URI uri, @Nullable RestTemplate restTemplate) {
70-
this(new ValueExpression<>(uri), restTemplate);
57+
this(new ValueExpression<>(uri), restTemplate != null ? RestClient.create(restTemplate) : null);
7158
}
7259

7360
/**
7461
* @deprecated Since 7.1 in favor of {@link RestClient}-based configuration.
7562
*/
7663
@Deprecated(since = "7.1", forRemoval = true)
7764
protected HttpMessageHandlerSpec(String uri, @Nullable RestTemplate restTemplate) {
78-
this(new LiteralExpression(uri), restTemplate);
65+
this(new LiteralExpression(uri), restTemplate != null ? RestClient.create(restTemplate) : null);
7966
}
8067

8168
/**
8269
* @deprecated Since 7.1 in favor of {@link RestClient}-based configuration.
8370
*/
8471
@Deprecated(since = "7.1", forRemoval = true)
8572
protected HttpMessageHandlerSpec(Expression uriExpression, @Nullable RestTemplate restTemplate) {
86-
super(restTemplate != null
87-
? new HttpRequestExecutingMessageHandler(uriExpression, RestClient.create(restTemplate))
88-
: new HttpRequestExecutingMessageHandler(uriExpression));
89-
this.clientSet = restTemplate != null;
73+
this(uriExpression, restTemplate != null ? RestClient.create(restTemplate) : null);
9074
}
9175

92-
protected HttpMessageHandlerSpec(URI uri, RestClient restClient) {
76+
protected HttpMessageHandlerSpec(URI uri, @Nullable RestClient restClient) {
9377
this(new ValueExpression<>(uri), restClient);
9478
}
9579

96-
protected HttpMessageHandlerSpec(String uri, RestClient restClient) {
80+
protected HttpMessageHandlerSpec(String uri, @Nullable RestClient restClient) {
9781
this(new LiteralExpression(uri), restClient);
9882
}
9983

100-
protected HttpMessageHandlerSpec(Expression uriExpression, RestClient restClient) {
84+
protected HttpMessageHandlerSpec(Expression uriExpression, @Nullable RestClient restClient) {
10185
super(new HttpRequestExecutingMessageHandler(uriExpression, restClient));
102-
this.clientSet = true;
86+
this.clientSet = restClient != null;
10387
}
10488

10589
/**

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

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ public class HttpRequestExecutingMessageHandler extends AbstractHttpRequestExecu
7171
@Nullable
7272
private final RestTemplate restTemplate;
7373

74-
@Nullable
75-
private volatile RestClient restClient;
74+
private volatile @Nullable RestClient restClient;
7675

7776
private final RestClient.@Nullable Builder localRestClientBuilder;
7877

@@ -92,18 +91,16 @@ public HttpRequestExecutingMessageHandler(URI uri) {
9291
* Create a handler that will send requests to the provided URI.
9392
* @param uri The URI.
9493
*/
95-
@SuppressWarnings("removal")
9694
public HttpRequestExecutingMessageHandler(String uri) {
97-
this(uri, (RestTemplate) null);
95+
this(uri, (RestClient) null);
9896
}
9997

10098
/**
10199
* Create a handler that will send requests to the provided URI Expression.
102100
* @param uriExpression The URI expression.
103101
*/
104-
@SuppressWarnings("removal")
105102
public HttpRequestExecutingMessageHandler(Expression uriExpression) {
106-
this(uriExpression, (RestTemplate) null);
103+
this(uriExpression, (RestClient) null);
107104
}
108105

109106
/**
@@ -132,20 +129,7 @@ public HttpRequestExecutingMessageHandler(String uri, @Nullable RestTemplate res
132129
*/
133130
@Deprecated(since = "7.1", forRemoval = true)
134131
public HttpRequestExecutingMessageHandler(Expression uriExpression, @Nullable RestTemplate restTemplate) {
135-
super(uriExpression);
136-
if (restTemplate != null) {
137-
this.restTemplate = restTemplate;
138-
this.localRestClientBuilder = null;
139-
this.restTemplateExplicitlySet = true;
140-
}
141-
else {
142-
this.restTemplate = null;
143-
this.localRestClientBuilder = RestClient.builder()
144-
.uriBuilderFactory(this.uriFactory);
145-
this.restTemplateExplicitlySet = false;
146-
}
147-
this.restClient = null;
148-
this.restClientExplicitlySet = false;
132+
this(uriExpression, restTemplate, null);
149133
}
150134

151135
/**
@@ -154,7 +138,7 @@ public HttpRequestExecutingMessageHandler(Expression uriExpression, @Nullable Re
154138
* @param restClient The rest client.
155139
* @since 7.1
156140
*/
157-
public HttpRequestExecutingMessageHandler(String uri, RestClient restClient) {
141+
public HttpRequestExecutingMessageHandler(String uri, @Nullable RestClient restClient) {
158142
this(new LiteralExpression(uri), restClient);
159143
/*
160144
* We'd prefer to do this assertion first, but the compiler doesn't allow it. However,
@@ -171,14 +155,38 @@ public HttpRequestExecutingMessageHandler(String uri, RestClient restClient) {
171155
* @param restClient The rest client.
172156
* @since 7.1
173157
*/
174-
public HttpRequestExecutingMessageHandler(Expression uriExpression, RestClient restClient) {
158+
public HttpRequestExecutingMessageHandler(Expression uriExpression, @Nullable RestClient restClient) {
159+
this(uriExpression, null, restClient);
160+
}
161+
162+
private HttpRequestExecutingMessageHandler(Expression uriExpression,
163+
@Nullable RestTemplate restTemplate, @Nullable RestClient restClient) {
164+
175165
super(uriExpression);
176-
Assert.notNull(restClient, "'restClient' must not be null");
177-
this.restTemplate = null;
178-
this.localRestClientBuilder = null;
179-
this.restClient = restClient;
180-
this.restTemplateExplicitlySet = false;
181-
this.restClientExplicitlySet = true;
166+
Assert.isTrue(restTemplate == null || restClient == null,
167+
"Only one of 'restTemplate' and 'restClient' may be provided");
168+
if (restClient != null) {
169+
this.restTemplate = null;
170+
this.localRestClientBuilder = null;
171+
this.restClient = restClient;
172+
this.restTemplateExplicitlySet = false;
173+
this.restClientExplicitlySet = true;
174+
}
175+
else if (restTemplate != null) {
176+
this.restTemplate = restTemplate;
177+
this.localRestClientBuilder = null;
178+
this.restClient = null;
179+
this.restTemplateExplicitlySet = true;
180+
this.restClientExplicitlySet = false;
181+
}
182+
else {
183+
this.restTemplate = null;
184+
this.localRestClientBuilder = RestClient.builder()
185+
.uriBuilderFactory(this.uriFactory);
186+
this.restClient = null;
187+
this.restTemplateExplicitlySet = false;
188+
this.restClientExplicitlySet = false;
189+
}
182190
}
183191

184192
@Override
@@ -224,12 +232,15 @@ public void setErrorHandler(ResponseErrorHandler errorHandler) {
224232
* @param messageConverters The message converters.
225233
* @see RestTemplate#setMessageConverters(java.util.List)
226234
*/
227-
@SuppressWarnings("removal")
228235
public void setMessageConverters(List<HttpMessageConverter<?>> messageConverters) {
229236
assertLocalClient("messageConverters");
230237
RestClient.Builder localRestClientBuilder = this.localRestClientBuilder;
231238
Assert.state(localRestClientBuilder != null, "'localRestClientBuilder' must not be null");
232-
localRestClientBuilder.messageConverters(messageConverters);
239+
localRestClientBuilder.configureMessageConverters((builder) ->
240+
builder.configureMessageConvertersList((converters) -> {
241+
converters.clear();
242+
converters.addAll(messageConverters);
243+
}));
233244
}
234245

235246
/**

spring-integration-http/src/test/java/org/springframework/integration/http/config/HttpOutboundGatewayParserTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050

5151
import static org.assertj.core.api.Assertions.assertThat;
5252
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
53+
import static org.assertj.core.api.Assertions.assertThatObject;
5354
import static org.mockito.Mockito.mock;
5455

5556
/**
@@ -172,7 +173,7 @@ public void fullConfig() {
172173
public void restClientConfig() {
173174
HttpRequestExecutingMessageHandler handler =
174175
(HttpRequestExecutingMessageHandler) this.restClientConfig.getHandler();
175-
assertThat(TestUtils.<RestClient>getPropertyValue(handler, "restClient")).isEqualTo(this.customRestClient);
176+
assertThatObject(TestUtils.getPropertyValue(handler, "restClient")).isSameAs(this.customRestClient);
176177
}
177178

178179
@Test

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@
7575
import org.springframework.validation.Validator;
7676
import org.springframework.web.client.DefaultResponseErrorHandler;
7777
import org.springframework.web.client.HttpClientErrorException;
78-
import org.springframework.web.client.RestClient;
7978
import org.springframework.web.context.WebApplicationContext;
8079
import org.springframework.web.multipart.MultipartResolver;
8180
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
@@ -147,13 +146,6 @@ public void testHttpProxyFlow() throws Exception {
147146
.andExpect(content().string("Error"));
148147
}
149148

150-
@Test
151-
public void restClientDslFactoryMethods() {
152-
RestClient restClient = RestClient.create();
153-
assertThat(Http.outboundGateway("http://localhost/test", restClient)).isNotNull();
154-
assertThat(Http.outboundChannelAdapter("http://localhost/test", restClient)).isNotNull();
155-
}
156-
157149
@Test
158150
public void testDynamicHttpEndpoint() throws Exception {
159151
IntegrationFlow flow =

0 commit comments

Comments
 (0)