Skip to content

Commit d5367ad

Browse files
committed
Add redirects method to RestTemplateBuilder
Add a `redirects` method to `RestTemplateBuilder` to make it easier to apply an existing `ClientHttpRequestFactorySettings` instance.
1 parent 364f014 commit d5367ad

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

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

+15
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.springframework.beans.BeanUtils;
3434
import org.springframework.boot.http.client.ClientHttpRequestFactoryBuilder;
3535
import org.springframework.boot.http.client.ClientHttpRequestFactorySettings;
36+
import org.springframework.boot.http.client.ClientHttpRequestFactorySettings.Redirects;
3637
import org.springframework.boot.ssl.SslBundle;
3738
import org.springframework.http.client.ClientHttpRequest;
3839
import org.springframework.http.client.ClientHttpRequestFactory;
@@ -64,6 +65,7 @@
6465
* @author Kevin Strijbos
6566
* @author Ilya Lukyanovich
6667
* @author Scott Frederick
68+
* @author Yanming Zhou
6769
* @since 1.4.0
6870
*/
6971
public class RestTemplateBuilder {
@@ -501,6 +503,19 @@ public RestTemplateBuilder readTimeout(Duration readTimeout) {
501503
this.defaultHeaders, this.customizers, this.requestCustomizers);
502504
}
503505

506+
/**
507+
* Sets the redirect strategy on the underlying {@link ClientHttpRequestFactory}.
508+
* @param redirects the redirect strategy
509+
* @return a new builder instance.
510+
* @since 3.4.1
511+
*/
512+
public RestTemplateBuilder redirects(Redirects redirects) {
513+
return new RestTemplateBuilder(this.requestFactorySettings.withRedirects(redirects), this.detectRequestFactory,
514+
this.rootUri, this.messageConverters, this.interceptors, this.requestFactoryBuilder,
515+
this.uriTemplateHandler, this.errorHandler, this.basicAuthentication, this.defaultHeaders,
516+
this.customizers, this.requestCustomizers);
517+
}
518+
504519
/**
505520
* Sets the SSL bundle on the underlying {@link ClientHttpRequestFactory}.
506521
* @param sslBundle the SSL bundle

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

+9
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.mockito.junit.jupiter.MockitoExtension;
3333

3434
import org.springframework.boot.http.client.ClientHttpRequestFactorySettings;
35+
import org.springframework.boot.http.client.ClientHttpRequestFactorySettings.Redirects;
3536
import org.springframework.http.HttpHeaders;
3637
import org.springframework.http.HttpMethod;
3738
import org.springframework.http.MediaType;
@@ -73,6 +74,7 @@
7374
* @author Kevin Strijbos
7475
* @author Ilya Lukyanovich
7576
* @author Brian Clozel
77+
* @author Yanming Zhou
7678
*/
7779
@ExtendWith(MockitoExtension.class)
7880
class RestTemplateBuilderTests {
@@ -486,6 +488,13 @@ void unwrappingDoesNotAffectRequestFactoryThatIsSetOnTheBuiltTemplate() {
486488
assertThat(template.getRequestFactory()).isInstanceOf(BufferingClientHttpRequestFactory.class);
487489
}
488490

491+
@Test
492+
void configureRedirects() {
493+
assertThat(this.builder.redirects(Redirects.DONT_FOLLOW)).extracting("requestFactorySettings")
494+
.extracting("redirects")
495+
.isSameAs(Redirects.DONT_FOLLOW);
496+
}
497+
489498
private ClientHttpRequest createRequest(RestTemplate template) {
490499
return ReflectionTestUtils.invokeMethod(template, "createRequest", URI.create("http://localhost"),
491500
HttpMethod.GET);

0 commit comments

Comments
 (0)