Skip to content

Commit 7690f06

Browse files
committed
GH-11268: Suppress RestTemplate removal warnings
Fixes: #11268 Keep @SuppressWarnings("removal") on remaining RestTemplate-based HTTP outbound APIs until that client is removed. Move HTTP outbound tests onto RestClient instead of suppressing the old client in tests. Signed-off-by: Burak KALAYCI <kalayciburak1996@gmail.com>
1 parent fe5e7d8 commit 7690f06

9 files changed

Lines changed: 220 additions & 231 deletions

File tree

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
* @author Artem Bilan
3838
* @author Shiliang Li
3939
* @author Arun Sethumadhavan
40+
* @author Burak Kalayci
4041
*
4142
* @since 5.0
4243
*/
@@ -90,6 +91,7 @@ public static HttpMessageHandlerSpec outboundChannelAdapter(Expression uriExpres
9091
* @deprecated Since 7.1 in favor of {@link RestClient}-based configuration.
9192
*/
9293
@Deprecated(since = "7.1", forRemoval = true)
94+
@SuppressWarnings("removal")
9395
public static HttpMessageHandlerSpec outboundChannelAdapter(URI uri, @Nullable RestTemplate restTemplate) {
9496
return outboundChannelAdapter(uri, toRestClient(restTemplate));
9597
}
@@ -115,6 +117,7 @@ public static HttpMessageHandlerSpec outboundChannelAdapter(URI uri, @Nullable R
115117
* @deprecated Since 7.1 in favor of {@link RestClient}-based configuration.
116118
*/
117119
@Deprecated(since = "7.1", forRemoval = true)
120+
@SuppressWarnings("removal")
118121
public static HttpMessageHandlerSpec outboundChannelAdapter(String uri, @Nullable RestTemplate restTemplate) {
119122
return outboundChannelAdapter(uri, toRestClient(restTemplate));
120123
}
@@ -142,6 +145,7 @@ public static HttpMessageHandlerSpec outboundChannelAdapter(String uri, @Nullabl
142145
* @deprecated Since 7.1 in favor of {@link RestClient}-based configuration.
143146
*/
144147
@Deprecated(since = "7.1", forRemoval = true)
148+
@SuppressWarnings("removal")
145149
public static <P> HttpMessageHandlerSpec outboundChannelAdapter(Function<Message<P>, ?> uriFunction,
146150
RestTemplate restTemplate) {
147151

@@ -174,6 +178,7 @@ public static <P> HttpMessageHandlerSpec outboundChannelAdapter(Function<Message
174178
* @deprecated Since 7.1 in favor of {@link RestClient}-based configuration.
175179
*/
176180
@Deprecated(since = "7.1", forRemoval = true)
181+
@SuppressWarnings("removal")
177182
public static HttpMessageHandlerSpec outboundChannelAdapter(Expression uriExpression,
178183
@Nullable RestTemplate restTemplate) {
179184

@@ -243,6 +248,7 @@ public static HttpMessageHandlerSpec outboundGateway(Expression uriExpression) {
243248
* @deprecated Since 7.1 in favor of {@link RestClient}-based configuration.
244249
*/
245250
@Deprecated(since = "7.1", forRemoval = true)
251+
@SuppressWarnings("removal")
246252
public static HttpMessageHandlerSpec outboundGateway(URI uri, @Nullable RestTemplate restTemplate) {
247253
return outboundGateway(uri, toRestClient(restTemplate));
248254
}
@@ -268,6 +274,7 @@ public static HttpMessageHandlerSpec outboundGateway(URI uri, @Nullable RestClie
268274
* @deprecated Since 7.1 in favor of {@link RestClient}-based configuration.
269275
*/
270276
@Deprecated(since = "7.1", forRemoval = true)
277+
@SuppressWarnings("removal")
271278
public static HttpMessageHandlerSpec outboundGateway(String uri, @Nullable RestTemplate restTemplate) {
272279
return outboundGateway(uri, toRestClient(restTemplate));
273280
}
@@ -295,6 +302,7 @@ public static HttpMessageHandlerSpec outboundGateway(String uri, @Nullable RestC
295302
* @deprecated Since 7.1 in favor of {@link RestClient}-based configuration.
296303
*/
297304
@Deprecated(since = "7.1", forRemoval = true)
305+
@SuppressWarnings("removal")
298306
public static <P> HttpMessageHandlerSpec outboundGateway(Function<Message<P>, ?> uriFunction,
299307
RestTemplate restTemplate) {
300308

@@ -327,6 +335,7 @@ public static <P> HttpMessageHandlerSpec outboundGateway(Function<Message<P>, ?>
327335
* @deprecated Since 7.1 in favor of {@link RestClient}-based configuration.
328336
*/
329337
@Deprecated(since = "7.1", forRemoval = true)
338+
@SuppressWarnings("removal")
330339
public static HttpMessageHandlerSpec outboundGateway(Expression uriExpression,
331340
@Nullable RestTemplate restTemplate) {
332341

@@ -360,6 +369,7 @@ private static HttpMessageHandlerSpec outboundGatewaySpec(Expression uriExpressi
360369
return new HttpMessageHandlerSpec(uriExpression, restClient);
361370
}
362371

372+
@SuppressWarnings("removal")
363373
private static @Nullable RestClient toRestClient(@Nullable RestTemplate restTemplate) {
364374
return restTemplate != null ? RestClient.create(restTemplate) : null;
365375
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
* @author Oleksii Komlyk
4343
* @author Arun Sethumadhavan
4444
* @author Glenn Renfro
45+
* @author Burak Kalayci
4546
*
4647
* @since 5.0
4748
*
@@ -56,6 +57,7 @@ public class HttpMessageHandlerSpec
5657
* @deprecated Since 7.1 in favor of {@link RestClient}-based configuration.
5758
*/
5859
@Deprecated(since = "7.1", forRemoval = true)
60+
@SuppressWarnings("removal")
5961
protected HttpMessageHandlerSpec(URI uri, @Nullable RestTemplate restTemplate) {
6062
this(new ValueExpression<>(uri), restTemplate != null ? RestClient.create(restTemplate) : null);
6163
}
@@ -64,6 +66,7 @@ protected HttpMessageHandlerSpec(URI uri, @Nullable RestTemplate restTemplate) {
6466
* @deprecated Since 7.1 in favor of {@link RestClient}-based configuration.
6567
*/
6668
@Deprecated(since = "7.1", forRemoval = true)
69+
@SuppressWarnings("removal")
6770
protected HttpMessageHandlerSpec(String uri, @Nullable RestTemplate restTemplate) {
6871
this(new LiteralExpression(uri), restTemplate != null ? RestClient.create(restTemplate) : null);
6972
}
@@ -72,6 +75,7 @@ protected HttpMessageHandlerSpec(String uri, @Nullable RestTemplate restTemplate
7275
* @deprecated Since 7.1 in favor of {@link RestClient}-based configuration.
7376
*/
7477
@Deprecated(since = "7.1", forRemoval = true)
78+
@SuppressWarnings("removal")
7579
protected HttpMessageHandlerSpec(Expression uriExpression, @Nullable RestTemplate restTemplate) {
7680
this(uriExpression, restTemplate != null ? RestClient.create(restTemplate) : null);
7781
}
@@ -109,6 +113,7 @@ public HttpMessageHandlerSpec requestFactory(ClientHttpRequestFactory requestFac
109113
* @deprecated Since 7.2 in favor of {@link RestClient.ResponseSpec.ErrorHandler}.
110114
*/
111115
@Deprecated(since = "7.2", forRemoval = true)
116+
@SuppressWarnings("removal")
112117
public HttpMessageHandlerSpec errorHandler(ResponseErrorHandler errorHandler) {
113118
Assert.isTrue(!isClientSet(), "the 'errorHandler' must be specified on the provided client");
114119
this.target.setErrorHandler(errorHandler);

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,13 @@
6767
* @author Shiliang Li
6868
* @author Arun Sethumadhavan
6969
* @author Glenn Renfro
70+
* @author Burak Kalayci
7071
*
7172
* @since 2.0
7273
*/
7374
public class HttpRequestExecutingMessageHandler extends AbstractHttpRequestExecutingMessageHandler {
7475

76+
@SuppressWarnings("removal")
7577
private final @Nullable RestTemplate restTemplate;
7678

7779
private volatile @Nullable RestClient restClient;
@@ -113,6 +115,7 @@ public HttpRequestExecutingMessageHandler(Expression uriExpression) {
113115
* @deprecated Since 7.1 in favor of {@link RestClient}-based configuration.
114116
*/
115117
@Deprecated(since = "7.1", forRemoval = true)
118+
@SuppressWarnings("removal")
116119
public HttpRequestExecutingMessageHandler(String uri, @Nullable RestTemplate restTemplate) {
117120
this(new LiteralExpression(uri), restTemplate);
118121
/*
@@ -131,6 +134,7 @@ public HttpRequestExecutingMessageHandler(String uri, @Nullable RestTemplate res
131134
* @deprecated Since 7.1 in favor of {@link RestClient}-based configuration.
132135
*/
133136
@Deprecated(since = "7.1", forRemoval = true)
137+
@SuppressWarnings("removal")
134138
public HttpRequestExecutingMessageHandler(Expression uriExpression, @Nullable RestTemplate restTemplate) {
135139
this(uriExpression, restTemplate, null);
136140
}
@@ -162,6 +166,7 @@ public HttpRequestExecutingMessageHandler(Expression uriExpression, @Nullable Re
162166
this(uriExpression, null, restClient);
163167
}
164168

169+
@SuppressWarnings("removal")
165170
private HttpRequestExecutingMessageHandler(Expression uriExpression,
166171
@Nullable RestTemplate restTemplate, @Nullable RestClient restClient) {
167172

@@ -226,6 +231,7 @@ protected void doInit() {
226231
* @see RestTemplate#setErrorHandler(ResponseErrorHandler)
227232
*/
228233
@Deprecated(since = "7.2", forRemoval = true)
234+
@SuppressWarnings("removal")
229235
public void setErrorHandler(ResponseErrorHandler errorHandler) {
230236
assertLocalClient("errorHandler");
231237
RestClient.Builder localRestClientBuilder = this.localRestClientBuilder;
@@ -325,6 +331,7 @@ public void setEncodingMode(DefaultUriBuilderFactory.EncodingMode encodingMode)
325331
}
326332
}
327333

334+
@SuppressWarnings("removal")
328335
private ResponseEntity<?> exchangeWithRestTemplate(Object uri, HttpMethod httpMethod, HttpEntity<?> httpRequest,
329336
Object expectedResponseType, Map<String, ?> uriVariables) {
330337

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

Lines changed: 77 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,24 @@
1616

1717
package org.springframework.integration.http;
1818

19+
import java.net.URI;
1920
import java.time.Instant;
2021
import java.time.ZoneId;
2122
import java.time.ZonedDateTime;
2223
import java.time.format.DateTimeFormatter;
2324
import java.util.Calendar;
2425
import java.util.Locale;
26+
import java.util.concurrent.atomic.AtomicReference;
27+
import java.util.function.Consumer;
28+
import java.util.function.Function;
2529

2630
import org.junit.jupiter.api.Test;
27-
import org.mockito.Mockito;
2831

2932
import org.springframework.beans.DirectFieldAccessor;
30-
import org.springframework.beans.PropertyAccessor;
3133
import org.springframework.beans.factory.annotation.Autowired;
3234
import org.springframework.beans.factory.annotation.Qualifier;
3335
import org.springframework.http.HttpEntity;
3436
import org.springframework.http.HttpHeaders;
35-
import org.springframework.http.HttpMethod;
3637
import org.springframework.http.HttpStatus;
3738
import org.springframework.http.ResponseEntity;
3839
import org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler;
@@ -45,7 +46,7 @@
4546
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
4647
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
4748
import org.springframework.util.MultiValueMap;
48-
import org.springframework.web.client.RestTemplate;
49+
import org.springframework.web.client.RestClient;
4950
import org.springframework.web.context.request.RequestAttributes;
5051
import org.springframework.web.context.request.RequestContextHolder;
5152
import org.springframework.web.context.request.ServletRequestAttributes;
@@ -54,11 +55,17 @@
5455
import org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter;
5556

5657
import static org.assertj.core.api.Assertions.assertThat;
58+
import static org.mockito.ArgumentMatchers.any;
59+
import static org.mockito.ArgumentMatchers.anyMap;
60+
import static org.mockito.ArgumentMatchers.anyString;
61+
import static org.mockito.Mockito.mock;
62+
import static org.mockito.Mockito.when;
5763

5864
/**
5965
* @author Artem Bilan
6066
* @author Gary Russell
6167
* @author Arun Sethumadhavan
68+
* @author Burak Kalayci
6269
*
6370
* @since 3.0
6471
*/
@@ -115,31 +122,19 @@ public void testHttpProxyScenario() throws Exception {
115122

116123
MockHttpServletResponse response = new MockHttpServletResponse();
117124

118-
RestTemplate template = Mockito.spy(new RestTemplate());
119-
120125
final String contentDispositionValue = "attachment; filename=\"test.txt\"";
121126

122-
Mockito.doAnswer(invocation -> {
123-
String uri = invocation.getArgument(0);
124-
assertThat(uri).isEqualTo("http://testServer/test?foo=bar&FOO=BAR");
125-
HttpEntity<?> httpEntity = (HttpEntity<?>) invocation.getArguments()[2];
126-
HttpHeaders httpHeaders = httpEntity.getHeaders();
127-
assertThat(httpHeaders.getIfModifiedSince()).isEqualTo(ifModifiedSince);
128-
assertThat(httpHeaders.getFirst("If-Unmodified-Since")).isEqualTo(ifUnmodifiedSinceValue);
129-
assertThat(httpHeaders.getFirst("Connection")).isEqualTo("Keep-Alive");
130-
131-
HttpHeaders responseHeaders = HttpHeaders.copyOf(httpHeaders);
132-
responseHeaders.set("Connection", "close");
133-
responseHeaders.set("Content-Disposition", contentDispositionValue);
134-
return new ResponseEntity<>(responseHeaders, HttpStatus.OK);
135-
}).when(template)
136-
.exchange(Mockito.anyString(), Mockito.any(HttpMethod.class),
137-
Mockito.any(HttpEntity.class), Mockito.<Class<?>>any(), Mockito.anyMap());
138-
139-
PropertyAccessor dfa = new DirectFieldAccessor(this.handler);
140-
dfa.setPropertyValue("localRestClientBuilder", null);
141-
dfa.setPropertyValue("restClient", null);
142-
dfa.setPropertyValue("restTemplate", template);
127+
injectRestClient(this.handler, httpEntity -> {
128+
HttpHeaders httpHeaders = httpEntity.getHeaders();
129+
assertThat(httpHeaders.getIfModifiedSince()).isEqualTo(ifModifiedSince);
130+
assertThat(httpHeaders.getFirst("If-Unmodified-Since")).isEqualTo(ifUnmodifiedSinceValue);
131+
assertThat(httpHeaders.getFirst("Connection")).isEqualTo("Keep-Alive");
132+
133+
HttpHeaders responseHeaders = HttpHeaders.copyOf(httpHeaders);
134+
responseHeaders.set("Connection", "close");
135+
responseHeaders.set("Content-Disposition", contentDispositionValue);
136+
return new ResponseEntity<>(responseHeaders, HttpStatus.OK);
137+
}, uri -> assertThat(uri).isEqualTo("http://testServer/test?foo=bar&FOO=BAR"));
143138

144139
RequestAttributes attributes = new ServletRequestAttributes(request);
145140
RequestContextHolder.setRequestAttributes(attributes);
@@ -176,33 +171,21 @@ public void testHttpMultipartProxyScenario() throws Exception {
176171

177172
MockHttpServletResponse response = new MockHttpServletResponse();
178173

179-
RestTemplate template = Mockito.spy(new RestTemplate());
180-
Mockito.doAnswer(invocation -> {
181-
String uri = invocation.getArgument(0);
182-
assertThat(uri).isEqualTo("http://testServer/testmp");
183-
HttpEntity<?> httpEntity = (HttpEntity<?>) invocation.getArguments()[2];
184-
HttpHeaders httpHeaders = httpEntity.getHeaders();
185-
assertThat(httpHeaders.getFirst("Connection")).isEqualTo("Keep-Alive");
186-
assertThat(httpHeaders.getContentType().toString())
187-
.isEqualTo("multipart/form-data;boundary=----WebKitFormBoundarywABD2xqC1FLBijlQ");
188-
189-
HttpEntity<?> entity = (HttpEntity<?>) invocation.getArguments()[2];
190-
assertThat(entity.getBody()).isInstanceOf(MultiValueMap.class);
191-
assertThat(((MultiValueMap<String, ?>) entity.getBody()).getFirst("foo"))
192-
.isEqualTo("foo".getBytes());
193-
194-
HttpHeaders responseHeaders = HttpHeaders.copyOf(httpHeaders);
195-
responseHeaders.set("Connection", "close");
196-
responseHeaders.set("Content-Type", "text/plain");
197-
return new ResponseEntity<>(responseHeaders, HttpStatus.OK);
198-
}).when(template)
199-
.exchange(Mockito.anyString(), Mockito.any(HttpMethod.class),
200-
Mockito.any(HttpEntity.class), Mockito.<Class<?>>any(), Mockito.anyMap());
201-
202-
PropertyAccessor dfa = new DirectFieldAccessor(this.handlermp);
203-
dfa.setPropertyValue("localRestClientBuilder", null);
204-
dfa.setPropertyValue("restClient", null);
205-
dfa.setPropertyValue("restTemplate", template);
174+
injectRestClient(this.handlermp, httpEntity -> {
175+
HttpHeaders httpHeaders = httpEntity.getHeaders();
176+
assertThat(httpHeaders.getFirst("Connection")).isEqualTo("Keep-Alive");
177+
assertThat(httpHeaders.getContentType().toString())
178+
.isEqualTo("multipart/form-data;boundary=----WebKitFormBoundarywABD2xqC1FLBijlQ");
179+
180+
assertThat(httpEntity.getBody()).isInstanceOf(MultiValueMap.class);
181+
assertThat(((MultiValueMap<String, ?>) httpEntity.getBody()).getFirst("foo"))
182+
.isEqualTo("foo".getBytes());
183+
184+
HttpHeaders responseHeaders = HttpHeaders.copyOf(httpHeaders);
185+
responseHeaders.set("Connection", "close");
186+
responseHeaders.set("Content-Type", "text/plain");
187+
return new ResponseEntity<>(responseHeaders, HttpStatus.OK);
188+
}, uri -> assertThat(uri).isEqualTo("http://testServer/testmp"));
206189

207190
RequestAttributes attributes = new ServletRequestAttributes(request);
208191
RequestContextHolder.setRequestAttributes(attributes);
@@ -215,4 +198,44 @@ public void testHttpMultipartProxyScenario() throws Exception {
215198
RequestContextHolder.resetRequestAttributes();
216199
}
217200

201+
@SuppressWarnings("unchecked")
202+
private static void injectRestClient(HttpRequestExecutingMessageHandler handler,
203+
Function<HttpEntity<?>, ResponseEntity<?>> exchange,
204+
Consumer<String> uriAsserter) {
205+
206+
RestClient restClient = mock(RestClient.class);
207+
RestClient.RequestBodyUriSpec spec = mock(RestClient.RequestBodyUriSpec.class);
208+
RestClient.ResponseSpec responseSpec = mock(RestClient.ResponseSpec.class);
209+
HttpHeaders headers = new HttpHeaders();
210+
AtomicReference<Object> body = new AtomicReference<>();
211+
AtomicReference<ResponseEntity<?>> response = new AtomicReference<>();
212+
213+
when(restClient.method(any())).thenReturn(spec);
214+
when(spec.uri(any(URI.class))).thenAnswer(invocation -> {
215+
uriAsserter.accept(invocation.getArgument(0).toString());
216+
return spec;
217+
});
218+
when(spec.uri(anyString(), anyMap())).thenAnswer(invocation -> {
219+
uriAsserter.accept(invocation.getArgument(0));
220+
return spec;
221+
});
222+
when(spec.headers(any())).thenAnswer(invocation -> {
223+
Consumer<HttpHeaders> headerConsumer = invocation.getArgument(0);
224+
headerConsumer.accept(headers);
225+
return spec;
226+
});
227+
when(spec.body(any(Object.class))).thenAnswer(invocation -> {
228+
body.set(invocation.getArgument(0));
229+
return spec;
230+
});
231+
when(spec.retrieve()).thenAnswer(invocation -> {
232+
response.set(exchange.apply(new HttpEntity<>(body.get(), headers)));
233+
return responseSpec;
234+
});
235+
when(responseSpec.toEntity(any(Class.class))).thenAnswer(invocation -> response.get());
236+
when(responseSpec.toBodilessEntity()).thenAnswer(invocation -> response.get());
237+
238+
new DirectFieldAccessor(handler).setPropertyValue("restClient", restClient);
239+
}
240+
218241
}

spring-integration-http/src/test/java/org/springframework/integration/http/config/HttpOutboundChannelAdapterParserTests-context.xml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@
1414

1515
<outbound-channel-adapter id="minimalConfig" url="http://localhost/test1" channel="requests"/>
1616

17-
<outbound-channel-adapter id="restTemplateConfig" url="http://localhost/test1" channel="requests"
18-
rest-template="customRestTemplate"/>
19-
20-
<beans:bean id="customRestTemplate" class="org.springframework.web.client.RestTemplate"/>
21-
2217
<outbound-channel-adapter id="restClientConfig" url="http://localhost/test1" channel="requests"
2318
rest-client="customRestClient"/>
2419

@@ -50,7 +45,7 @@
5045
<outbound-channel-adapter id="withUrlAndTemplate"
5146
url="http://localhost/test1" channel="requests"
5247
uri-variables-expression="@uriVariables"
53-
rest-template="customRestTemplate"/>
48+
rest-client="customRestClient"/>
5449

5550
<outbound-channel-adapter id="withUrlExpression" url-expression="'http://localhost/test1'" channel="requests"/>
5651

0 commit comments

Comments
 (0)