Skip to content

Commit eaedef4

Browse files
committed
Add BeanFactory-based implementation for RestTemplate.
1 parent 6850ae9 commit eaedef4

File tree

6 files changed

+82
-50
lines changed

6 files changed

+82
-50
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/interfaceclients/http/AbstractHttpInterfaceClientsFactoryBean.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ public Object getObject() throws Exception {
3636
return proxyFactory.createClient(this.type);
3737
}
3838

39+
protected String getBaseUrl() {
40+
HttpInterfaceClientsProperties properties = this.applicationContext
41+
.getBean(HttpInterfaceClientsProperties.class);
42+
43+
return properties.getProperties(this.clientId).getBaseUrl();
44+
}
45+
3946
private HttpServiceProxyFactory proxyFactory() {
4047
HttpServiceProxyFactory userProvidedProxyFactory = QualifiedBeanProvider
4148
.qualifiedBean(this.applicationContext.getBeanFactory(), HttpServiceProxyFactory.class, this.clientId);

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/interfaceclients/http/HttpInterfaceClientsAutoConfiguration.java

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
import org.springframework.context.annotation.Configuration;
2929
import org.springframework.context.annotation.Import;
3030
import org.springframework.web.client.RestClient;
31+
import org.springframework.web.client.RestTemplate;
3132
import org.springframework.web.client.support.RestClientAdapter;
33+
import org.springframework.web.client.support.RestTemplateAdapter;
3234
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
3335

3436
/**
@@ -49,26 +51,16 @@ public class HttpInterfaceClientsAutoConfiguration {
4951
protected static class RestClientAdapterProviderConfiguration {
5052

5153
}
52-
//
53-
// @Configuration(proxyBeanMethods = false)
54-
// @ConditionalOnClass({ RestTemplate.class, RestTemplateAdapter.class,
55-
// HttpServiceProxyFactory.class })
56-
// @Conditional(NotReactiveWebApplicationCondition.class)
57-
// protected static class RestTemplateAdapterProviderConfiguration {
58-
//
59-
// @Bean
60-
// @ConditionalOnBean(RestTemplateBuilder.class)
61-
// @ConditionalOnMissingBean
62-
// @ConditionalOnProperty(value = "spring.interfaceclients.resttemplate.enabled",
63-
// havingValue = "true")
64-
// HttpExchangeAdapterProvider restTemplateAdapterProvider(RestTemplateBuilder
65-
// restTemplateBuilder,
66-
// ObjectProvider<HttpInterfaceClientsProperties> propertiesProvider) {
67-
// return new RestTemplateAdapterProvider(restTemplateBuilder, propertiesProvider);
68-
// }
69-
//
70-
// }
71-
//
54+
55+
@Configuration(proxyBeanMethods = false)
56+
@ConditionalOnClass({ RestTemplate.class, RestTemplateAdapter.class, HttpServiceProxyFactory.class })
57+
@Conditional(NotReactiveWebApplicationCondition.class)
58+
@ConditionalOnProperty(value = "spring.interfaceclients.resttemplate.enabled", havingValue = "true")
59+
@Import(RestTemplateInterfaceClientsImportRegistrar.class)
60+
protected static class RestTemplateAdapterProviderConfiguration {
61+
62+
}
63+
7264
// @Configuration(proxyBeanMethods = false)
7365
// @ConditionalOnClass({ WebClient.class, WebClientAdapter.class,
7466
// HttpServiceProxyFactory.class })

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/interfaceclients/http/QualifiedBeanProvider.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
2929

3030
/**
31+
* @author Josh Long
3132
* @author Olga Maciaszek-Sharma
3233
*/
3334
final class QualifiedBeanProvider {

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/interfaceclients/http/RestClientInterfaceClientsFactoryBean.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,13 @@ public class RestClientInterfaceClientsFactoryBean extends AbstractHttpInterface
3232

3333
@Override
3434
protected HttpExchangeAdapter exchangeAdapter() {
35-
HttpInterfaceClientsProperties properties = this.applicationContext
36-
.getBean(HttpInterfaceClientsProperties.class);
37-
// If the user wants to set the baseUrl directly on the builder,
38-
// it should not be set in properties.
39-
String baseUrl = properties.getProperties(this.clientId).getBaseUrl();
35+
String baseUrl = getBaseUrl();
4036

4137
RestClient userProvidedRestClient = QualifiedBeanProvider
4238
.qualifiedBean(this.applicationContext.getBeanFactory(), RestClient.class, this.clientId);
4339
if (userProvidedRestClient != null) {
40+
// If the user wants to set the baseUrl directly on the builder,
41+
// it should not be set in properties.
4442
if (baseUrl != null) {
4543
userProvidedRestClient = userProvidedRestClient.mutate().baseUrl(baseUrl).build();
4644
}
Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,54 +19,50 @@
1919
import org.apache.commons.logging.Log;
2020
import org.apache.commons.logging.LogFactory;
2121

22-
import org.springframework.beans.factory.ObjectProvider;
23-
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
2422
import org.springframework.boot.web.client.RestTemplateBuilder;
2523
import org.springframework.web.client.RestTemplate;
2624
import org.springframework.web.client.support.RestTemplateAdapter;
2725
import org.springframework.web.service.invoker.HttpExchangeAdapter;
26+
import org.springframework.web.util.DefaultUriBuilderFactory;
2827

2928
/**
3029
* @author Olga Maciaszek-Sharma
3130
*/
32-
public class RestTemplateAdapterProvider implements HttpExchangeAdapterProvider {
31+
public class RestTemplateInterfaceClientsFactoryBean extends AbstractHttpInterfaceClientsFactoryBean {
3332

34-
private static final Log logger = LogFactory.getLog(RestTemplateAdapterProvider.class);
35-
36-
private final RestTemplateBuilder restTemplateBuilder;
37-
38-
private final ObjectProvider<HttpInterfaceClientsProperties> propertiesProvider;
39-
40-
public RestTemplateAdapterProvider(RestTemplateBuilder restTemplateBuilder,
41-
ObjectProvider<HttpInterfaceClientsProperties> propertiesProvider) {
42-
this.restTemplateBuilder = restTemplateBuilder;
43-
this.propertiesProvider = propertiesProvider;
44-
}
33+
private static final Log logger = LogFactory.getLog(RestTemplateInterfaceClientsFactoryBean.class);
4534

4635
@Override
47-
public HttpExchangeAdapter get(ConfigurableListableBeanFactory beanFactory, String clientId) {
48-
RestTemplate userProvidedRestTemplate = QualifiedBeanProvider.qualifiedBean(beanFactory, RestTemplate.class,
49-
clientId);
36+
protected HttpExchangeAdapter exchangeAdapter() {
37+
String baseUrl = getBaseUrl();
38+
39+
RestTemplate userProvidedRestTemplate = QualifiedBeanProvider
40+
.qualifiedBean(this.applicationContext.getBeanFactory(), RestTemplate.class, this.clientId);
5041
if (userProvidedRestTemplate != null) {
42+
// If the user wants to set the baseUrl directly on the builder,
43+
// it should not be set in properties.
44+
if (baseUrl != null) {
45+
userProvidedRestTemplate.setUriTemplateHandler(new DefaultUriBuilderFactory(baseUrl));
46+
}
5147
return RestTemplateAdapter.create(userProvidedRestTemplate);
5248
}
53-
HttpInterfaceClientsProperties properties = this.propertiesProvider.getObject();
54-
String baseUrl = properties.getProperties(clientId).getBaseUrl();
55-
RestTemplateBuilder userProvidedRestTemplateBuilder = QualifiedBeanProvider.qualifiedBean(beanFactory,
56-
RestTemplateBuilder.class, clientId);
49+
50+
RestTemplateBuilder userProvidedRestTemplateBuilder = QualifiedBeanProvider
51+
.qualifiedBean(this.applicationContext.getBeanFactory(), RestTemplateBuilder.class, this.clientId);
5752
if (userProvidedRestTemplateBuilder != null) {
58-
// If the user wants to set the baseUrl directly on the builder,
59-
// it should not be set in properties.
53+
6054
if (baseUrl != null) {
6155
userProvidedRestTemplateBuilder.rootUri(baseUrl);
6256
}
6357
return RestTemplateAdapter.create(userProvidedRestTemplateBuilder.build());
6458
}
59+
6560
// create a RestTemplateAdapter bean with default implementation
6661
if (logger.isDebugEnabled()) {
67-
logger.debug("Creating RestTemplateAdapter for '" + clientId + "'");
62+
logger.debug("Creating RestTemplateAdapter for '" + this.clientId + "'");
6863
}
69-
RestTemplate restTemplate = this.restTemplateBuilder.rootUri(baseUrl).build();
64+
RestTemplateBuilder builder = this.applicationContext.getBean(RestTemplateBuilder.class);
65+
RestTemplate restTemplate = builder.rootUri(baseUrl).build();
7066
return RestTemplateAdapter.create(restTemplate);
7167
}
7268

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2012-2024 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.interfaceclients.http;
18+
19+
import java.lang.annotation.Annotation;
20+
21+
import org.springframework.boot.autoconfigure.interfaceclients.AbstractInterfaceClientsImportRegistrar;
22+
23+
/**
24+
* @author Olga Maciaszek-Sharma
25+
*/
26+
public class RestTemplateInterfaceClientsImportRegistrar extends AbstractInterfaceClientsImportRegistrar {
27+
28+
@Override
29+
protected Class<? extends Annotation> getAnnotation() {
30+
return HttpClient.class;
31+
}
32+
33+
@Override
34+
protected Class<?> getFactoryBeanClass() {
35+
return RestTemplateInterfaceClientsFactoryBean.class;
36+
}
37+
38+
}

0 commit comments

Comments
 (0)