Skip to content

Commit 3ac034e

Browse files
committed
Fail when setReadTimeout on httpclient5 request factory
Prior to this commit, the `RestTemplateBuilder` would offer a generic `setReadTimeout` method to configure the read timeout on the underlying `ClientHttpRequestFactory`. This would be done in a reflective fashion, considering that all implementations align with this behavior. This option cannot be provided for HttpClient5 at the `ClientHttpRequestFactory` level anymore, so this has been deprecated in Spring Framework 6.0 and will log a warning. In order to align with our existing behavior (throwing exceptions if the option cannot be set), this commit ensures that exceptions are also thrown if the method is marked as deprecated. See gh-32461
1 parent 58f3054 commit 3ac034e

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/client/RestTemplateBuilder.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -783,11 +783,15 @@ private void setBufferRequestBody(ClientHttpRequestFactory factory) {
783783

784784
private Method findMethod(ClientHttpRequestFactory requestFactory, String methodName, Class<?>... parameters) {
785785
Method method = ReflectionUtils.findMethod(requestFactory.getClass(), methodName, parameters);
786-
if (method != null) {
787-
return method;
786+
if (method == null) {
787+
throw new IllegalStateException("Request factory " + requestFactory.getClass()
788+
+ " does not have a suitable " + methodName + " method");
788789
}
789-
throw new IllegalStateException("Request factory " + requestFactory.getClass()
790-
+ " does not have a suitable " + methodName + " method");
790+
else if (method.isAnnotationPresent(Deprecated.class)) {
791+
throw new IllegalStateException("Request factory " + requestFactory.getClass() + " has the "
792+
+ methodName + " method marked as deprecated");
793+
}
794+
return method;
791795
}
792796

793797
private void invoke(ClientHttpRequestFactory requestFactory, Method method, Object... parameters) {

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/client/RestTemplateBuilderTests.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import java.util.function.Supplier;
2626

2727
import okhttp3.OkHttpClient;
28-
import org.apache.http.client.config.RequestConfig;
2928
import org.assertj.core.api.InstanceOfAssertFactories;
3029
import org.junit.jupiter.api.Test;
3130
import org.junit.jupiter.api.extension.ExtendWith;
@@ -63,6 +62,7 @@
6362
import static org.assertj.core.api.Assertions.assertThat;
6463
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
6564
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
65+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
6666
import static org.assertj.core.api.Assertions.entry;
6767
import static org.mockito.ArgumentMatchers.any;
6868
import static org.mockito.BDDMockito.then;
@@ -81,6 +81,7 @@
8181
* @author Dmytro Nosan
8282
* @author Kevin Strijbos
8383
* @author Ilya Lukyanovich
84+
* @author Brian Clozel
8485
*/
8586
@ExtendWith(MockitoExtension.class)
8687
class RestTemplateBuilderTests {
@@ -477,17 +478,14 @@ void connectTimeoutCanBeConfiguredOnHttpComponentsRequestFactory() {
477478
ClientHttpRequestFactory requestFactory = this.builder
478479
.requestFactory(HttpComponentsClientHttpRequestFactory.class).setConnectTimeout(Duration.ofMillis(1234))
479480
.build().getRequestFactory();
480-
assertThat(((RequestConfig) ReflectionTestUtils.getField(requestFactory, "requestConfig")).getConnectTimeout())
481-
.isEqualTo(1234);
481+
assertThat(((int) ReflectionTestUtils.getField(requestFactory, "connectTimeout"))).isEqualTo(1234);
482482
}
483483

484484
@Test
485-
void readTimeoutCanBeConfiguredOnHttpComponentsRequestFactory() {
486-
ClientHttpRequestFactory requestFactory = this.builder
487-
.requestFactory(HttpComponentsClientHttpRequestFactory.class).setReadTimeout(Duration.ofMillis(1234))
488-
.build().getRequestFactory();
489-
assertThat(((RequestConfig) ReflectionTestUtils.getField(requestFactory, "requestConfig")).getSocketTimeout())
490-
.isEqualTo(1234);
485+
void readTimeoutConfigurationFailsOnHttpComponentsRequestFactory() {
486+
assertThatThrownBy(() -> this.builder.requestFactory(HttpComponentsClientHttpRequestFactory.class)
487+
.setReadTimeout(Duration.ofMillis(1234)).build()).isInstanceOf(IllegalStateException.class)
488+
.hasMessageContaining("setReadTimeout method marked as deprecated");
491489
}
492490

493491
@Test

0 commit comments

Comments
 (0)