Skip to content

Commit e151c56

Browse files
committed
Make RestClientCustomizer a Selectable
Update `RestClientCustomizer` so that it can be used as a fluent API to select specific `RestClient` instances.
1 parent 673ee5e commit e151c56

File tree

2 files changed

+74
-7
lines changed

2 files changed

+74
-7
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
import org.springframework.boot.http.client.ClientHttpRequestFactoryBuilder;
2222
import org.springframework.boot.http.client.ClientHttpRequestFactorySettings;
23+
import org.springframework.boot.selector.Selectable;
24+
import org.springframework.boot.selector.Selector;
2325
import org.springframework.boot.web.client.RestClientCustomizer;
2426
import org.springframework.web.client.RestClient;
2527
import org.springframework.web.client.RestClient.Builder;
@@ -28,6 +30,7 @@
2830
* Configure {@link Builder RestClient.Builder} with sensible defaults.
2931
*
3032
* @author Moritz Halbritter
33+
* @author Phillip Webb
3134
* @since 3.2.0
3235
*/
3336
public class RestClientBuilderConfigurer {
@@ -64,12 +67,9 @@ public RestClient.Builder configure(RestClient.Builder builder) {
6467
return builder;
6568
}
6669

67-
private void applyCustomizers(Builder builder) {
68-
if (this.customizers != null) {
69-
for (RestClientCustomizer customizer : this.customizers) {
70-
customizer.customize(builder);
71-
}
72-
}
70+
private void applyCustomizers(RestClient.Builder builder) {
71+
Selector.streamSelected(this.customizers, Selectable.blank())
72+
.forEach((customizer) -> customizer.customize(builder));
7373
}
7474

7575
}

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

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,23 @@
1616

1717
package org.springframework.boot.web.client;
1818

19+
import java.util.function.Predicate;
20+
21+
import org.springframework.boot.selector.Selectable;
22+
import org.springframework.boot.selector.Selector;
1923
import org.springframework.web.client.RestClient;
24+
import org.springframework.web.client.RestClient.Builder;
2025

2126
/**
2227
* Callback interface that can be used to customize a
2328
* {@link org.springframework.web.client.RestClient.Builder RestClient.Builder}.
2429
*
2530
* @author Arjen Poutsma
31+
* @author Phillip Webb
2632
* @since 3.2.0
2733
*/
2834
@FunctionalInterface
29-
public interface RestClientCustomizer {
35+
public interface RestClientCustomizer extends Selector<RestClientCustomizer> {
3036

3137
/**
3238
* Callback to customize a {@link org.springframework.web.client.RestClient.Builder
@@ -35,4 +41,65 @@ public interface RestClientCustomizer {
3541
*/
3642
void customize(RestClient.Builder restClientBuilder);
3743

44+
/**
45+
* Callback to customize a {@link org.springframework.web.client.RestClient.Builder
46+
* RestClient.Builder} instance for a selected {@link Selectable}.
47+
* @param restClientBuilder the client builder to customize
48+
* @param selected the {@link #selects(Selectable) selected} selectable
49+
*/
50+
default void customize(RestClient.Builder restClientBuilder, Selectable selected) {
51+
customize(restClientBuilder);
52+
}
53+
54+
@Override
55+
default RestClientCustomizer onlyWhen(Predicate<Selectable> predicate) {
56+
return new ForSelected() {
57+
58+
@Override
59+
public boolean selects(Selectable selectable) {
60+
return (selectable != null) && RestClientCustomizer.this.selects(selectable)
61+
&& predicate.test(selectable);
62+
}
63+
64+
@Override
65+
public void customize(RestClient.Builder restClientBuilder, Selectable selectable) {
66+
RestClientCustomizer.this.customize(restClientBuilder, selectable);
67+
}
68+
69+
};
70+
}
71+
72+
/**
73+
* Helper method that can be used to create a {@link RestClientCustomizer} from a
74+
* lambda for method chaining.
75+
* @param customizer the source customizer
76+
* @return the customizer
77+
*/
78+
static RestClientCustomizer of(RestClientCustomizer customizer) {
79+
return customizer;
80+
}
81+
82+
/**
83+
* Helper method that can be used to create a {@link RestClientCustomizer} from a
84+
* lambda for method chaining.
85+
* @param customizer the source customizer
86+
* @return the customizer
87+
*/
88+
static RestClientCustomizer of(ForSelected customizer) {
89+
return customizer;
90+
}
91+
92+
/**
93+
* {@link RestClientCustomizer} that can be used when the selected {@link Selectable}
94+
* is needed during customization`.
95+
*/
96+
interface ForSelected extends RestClientCustomizer {
97+
98+
@Override
99+
default void customize(Builder restClientBuilder) {
100+
customize(restClientBuilder, Selectable.blank());
101+
}
102+
103+
}
104+
38105
}

0 commit comments

Comments
 (0)