Skip to content

Commit 49a4fdd

Browse files
committed
Initial support for RestClient
This commit introduces support for Spring's RestClient, in the form of a RestClientCustomizer and RestClientAutoConfiguration. Integration with RestTemplateBuilder and HttpMessageConvertersAutoConfiguration has been discussed with Andy, but is beyond the scope of my Spring Boot knowledge.
1 parent 2350d9c commit 49a4fdd

File tree

3 files changed

+222
-0
lines changed

3 files changed

+222
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.web.client;
18+
19+
import org.springframework.beans.factory.ObjectProvider;
20+
import org.springframework.boot.autoconfigure.AutoConfiguration;
21+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
22+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
23+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
24+
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
25+
import org.springframework.boot.autoconfigure.condition.NoneNestedConditions;
26+
import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration;
27+
import org.springframework.boot.web.client.RestClientCustomizer;
28+
import org.springframework.context.annotation.Bean;
29+
import org.springframework.context.annotation.Conditional;
30+
import org.springframework.context.annotation.Scope;
31+
import org.springframework.web.client.RestClient;
32+
33+
/**
34+
* {@link EnableAutoConfiguration Auto-configuration} for {@link RestClient}.
35+
* <p>
36+
* This will produce a {@link RestClient.Builder RestClient.Builder} bean with the
37+
* {@code prototype} scope, meaning each injection point will receive a newly cloned
38+
* instance of the builder.
39+
*
40+
* @author Arjen Poutsma
41+
* @since 3.2.0
42+
*/
43+
@AutoConfiguration(after = HttpMessageConvertersAutoConfiguration.class)
44+
@ConditionalOnClass(RestClient.class)
45+
@Conditional(RestClientAutoConfiguration.NotReactiveWebApplicationCondition.class)
46+
public class RestClientAutoConfiguration {
47+
48+
@Bean
49+
@Scope("prototype")
50+
@ConditionalOnMissingBean
51+
public RestClient.Builder webClientBuilder(ObjectProvider<RestClientCustomizer> customizerProvider) {
52+
RestClient.Builder builder = RestClient.builder();
53+
customizerProvider.orderedStream().forEach((customizer) -> customizer.customize(builder));
54+
return builder;
55+
}
56+
57+
static class NotReactiveWebApplicationCondition extends NoneNestedConditions {
58+
59+
NotReactiveWebApplicationCondition() {
60+
super(ConfigurationPhase.PARSE_CONFIGURATION);
61+
}
62+
63+
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
64+
private static class ReactiveWebApplication {
65+
66+
}
67+
68+
}
69+
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.web.client;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.boot.autoconfigure.AutoConfigurations;
22+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
23+
import org.springframework.boot.web.client.RestClientCustomizer;
24+
import org.springframework.boot.web.codec.CodecCustomizer;
25+
import org.springframework.context.annotation.Bean;
26+
import org.springframework.context.annotation.Configuration;
27+
import org.springframework.web.client.RestClient;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
import static org.mockito.ArgumentMatchers.any;
31+
import static org.mockito.BDDMockito.then;
32+
import static org.mockito.Mockito.mock;
33+
34+
/**
35+
* Tests for {@link RestClientAutoConfiguration}
36+
*
37+
* @author Arjen Poutsma
38+
*/
39+
class RestClientAutoConfigurationTests {
40+
41+
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
42+
.withConfiguration(AutoConfigurations.of(RestClientAutoConfiguration.class));
43+
44+
@Test
45+
void shouldCreateBuilder() {
46+
this.contextRunner.run((context) -> {
47+
RestClient.Builder builder = context.getBean(RestClient.Builder.class);
48+
RestClient restClient = builder.build();
49+
assertThat(restClient).isNotNull();
50+
});
51+
}
52+
53+
@Test
54+
void restClientShouldApplyCustomizers() {
55+
this.contextRunner.withUserConfiguration(RestClientCustomizerConfig.class).run((context) -> {
56+
RestClient.Builder builder = context.getBean(RestClient.Builder.class);
57+
RestClientCustomizer customizer = context.getBean("webClientCustomizer", RestClientCustomizer.class);
58+
builder.build();
59+
then(customizer).should().customize(any(RestClient.Builder.class));
60+
});
61+
}
62+
63+
@Test
64+
void shouldGetPrototypeScopedBean() {
65+
this.contextRunner.withUserConfiguration(RestClientCustomizerConfig.class).run((context) -> {
66+
RestClient.Builder firstBuilder = context.getBean(RestClient.Builder.class);
67+
RestClient.Builder secondBuilder = context.getBean(RestClient.Builder.class);
68+
assertThat(firstBuilder).isNotEqualTo(secondBuilder);
69+
});
70+
}
71+
72+
@Test
73+
void shouldNotCreateClientBuilderIfAlreadyPresent() {
74+
this.contextRunner.withUserConfiguration(CustomRestClientBuilderConfig.class).run((context) -> {
75+
RestClient.Builder builder = context.getBean(RestClient.Builder.class);
76+
assertThat(builder).isInstanceOf(MyWebClientBuilder.class);
77+
});
78+
}
79+
80+
@Configuration(proxyBeanMethods = false)
81+
static class CodecConfiguration {
82+
83+
@Bean
84+
CodecCustomizer myCodecCustomizer() {
85+
return mock(CodecCustomizer.class);
86+
}
87+
88+
}
89+
90+
@Configuration(proxyBeanMethods = false)
91+
static class RestClientCustomizerConfig {
92+
93+
@Bean
94+
RestClientCustomizer webClientCustomizer() {
95+
return mock(RestClientCustomizer.class);
96+
}
97+
98+
}
99+
100+
@Configuration(proxyBeanMethods = false)
101+
static class CustomRestClientBuilderConfig {
102+
103+
@Bean
104+
MyWebClientBuilder myWebClientBuilder() {
105+
return mock(MyWebClientBuilder.class);
106+
}
107+
108+
}
109+
110+
interface MyWebClientBuilder extends RestClient.Builder {
111+
112+
}
113+
114+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.web.client;
18+
19+
import org.springframework.web.client.RestClient;
20+
21+
/**
22+
* Callback interface that can be used to customize a
23+
* {@link org.springframework.web.client.RestClient.Builder RestClient.Builder}.
24+
*
25+
* @author Arjen Poutsma
26+
* @since 3.2.0
27+
*/
28+
@FunctionalInterface
29+
public interface RestClientCustomizer {
30+
31+
/**
32+
* Callback to customize a {@link org.springframework.web.client.RestClient.Builder
33+
* RestClient.Builder} instance.
34+
* @param restClientBuilder the client builder to customize
35+
*/
36+
void customize(RestClient.Builder restClientBuilder);
37+
38+
}

0 commit comments

Comments
 (0)