Browse Source

Add `requestFactorySettings` method to `RestTemplateBuilder`

Add a `requestFactorySettings` method to `RestTemplateBuilder` to make
it easier to apply an existing `ClientHttpRequestFactorySettings`
instance.

Closes gh-42885
pull/42889/head
Phillip Webb 1 year ago
parent
commit
022f3cb019
  1. 16
      spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/client/RestTemplateBuilder.java
  2. 15
      spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/client/RestTemplateBuilderTests.java

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

@ -429,6 +429,22 @@ public class RestTemplateBuilder { @@ -429,6 +429,22 @@ public class RestTemplateBuilder {
this.customizers, this.requestCustomizers);
}
/**
* Sets the {@link ClientHttpRequestFactorySettings}. This will replace any previously
* set {@link #connectTimeout(Duration) connectTimeout} ,{@link #readTimeout(Duration)
* readTimeout} and {@link #sslBundle(SslBundle) sslBundle} values.
* @param requestFactorySettings the request factory settings
* @return a new builder instance
* @since 3.4.0
*/
public RestTemplateBuilder requestFactorySettings(ClientHttpRequestFactorySettings requestFactorySettings) {
Assert.notNull(requestFactorySettings, "ClientHttpRequestFactorySettings must not be null");
return new RestTemplateBuilder(requestFactorySettings, this.detectRequestFactory, this.rootUri,
this.messageConverters, this.interceptors, this.requestFactoryBuilder, this.uriTemplateHandler,
this.errorHandler, this.basicAuthentication, this.defaultHeaders, this.customizers,
this.requestCustomizers);
}
/**
* Sets the connection timeout on the underlying {@link ClientHttpRequestFactory}.
* @param connectTimeout the connection timeout

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

@ -18,6 +18,7 @@ package org.springframework.boot.web.client; @@ -18,6 +18,7 @@ package org.springframework.boot.web.client;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.Arrays;
import java.util.Collections;
import java.util.Set;
@ -30,6 +31,7 @@ import org.mockito.InOrder; @@ -30,6 +31,7 @@ import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.boot.http.client.ClientHttpRequestFactorySettings;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
@ -272,9 +274,8 @@ class RestTemplateBuilderTests { @@ -272,9 +274,8 @@ class RestTemplateBuilderTests {
@Test
void requestFactoryWhenFunctionIsNullShouldThrowException() {
assertThatIllegalArgumentException()
.isThrownBy(() -> this.builder
.requestFactory((Function<ClientHttpRequestFactorySettings, ClientHttpRequestFactory>) null))
assertThatIllegalArgumentException().isThrownBy(() -> this.builder.requestFactory(
(Function<org.springframework.boot.web.client.ClientHttpRequestFactorySettings, ClientHttpRequestFactory>) null))
.withMessageContaining("RequestFactoryFunction must not be null");
}
@ -343,6 +344,14 @@ class RestTemplateBuilderTests { @@ -343,6 +344,14 @@ class RestTemplateBuilderTests {
assertThat(request.getHeaders()).contains(entry("spring", Collections.singletonList("boot")));
}
@Test
void requestFactorySettingsAppliesSettings() {
ClientHttpRequestFactorySettings settings = ClientHttpRequestFactorySettings.defaults()
.withConnectTimeout(Duration.ofSeconds(1));
RestTemplate template = this.builder.requestFactorySettings(settings).build();
assertThat(template.getRequestFactory()).extracting("connectTimeout").isEqualTo(1000L);
}
@Test
void requestCustomizersAddsCustomizers() {
RestTemplate template = this.builder

Loading…
Cancel
Save