Skip to content

Commit 1c85eb0

Browse files
committed
Support Elasticsearch RestClientBuilder auto-configuration without RestHighLevelClient
Prior to this commit, Spring Boot would only auto-configure the `RestHighLevelClient` and `RestClientBuilder` if the `RestHighLevelClient` was present. This was done in 1d73d4e. This commit brings back the exposing of the `RestClient` bean in Spring Boot when exposing the `RestHighLevelClient` or when the `RestHighLevelClient` is not present. It allows for using the Spring Boot auto configuration and its customizers of the `RestClientBuilder` in a similar way as it is done for the `RestTeamplateBuilder` and the `WebClient.Builder`. Now the presence of the `org.elasticsearch.client:elasticsearch-rest-high-level-client` is optional. This opens the door for potentially adding support to the new [Elasticsearch Java Client](https://github.com/elastic/elasticsearch-java) that is based on the same `RestClient`
1 parent 83e4430 commit 1c85eb0

File tree

3 files changed

+44
-9
lines changed

3 files changed

+44
-9
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientAutoConfiguration.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@
1717
package org.springframework.boot.autoconfigure.elasticsearch;
1818

1919
import org.elasticsearch.client.RestClient;
20-
import org.elasticsearch.client.RestHighLevelClient;
2120

2221
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2322
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
24-
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2523
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientConfigurations.RestClientBuilderConfiguration;
24+
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientConfigurations.RestClientConfiguration;
2625
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientConfigurations.RestClientSnifferConfiguration;
2726
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientConfigurations.RestHighLevelClientConfiguration;
2827
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@@ -38,11 +37,11 @@
3837
*/
3938
@SuppressWarnings("deprecation")
4039
@Configuration(proxyBeanMethods = false)
41-
@ConditionalOnClass(RestHighLevelClient.class)
42-
@ConditionalOnMissingBean(RestClient.class)
40+
@ConditionalOnClass(RestClient.class)
4341
@EnableConfigurationProperties({ ElasticsearchProperties.class, ElasticsearchRestClientProperties.class,
4442
DeprecatedElasticsearchRestClientProperties.class })
4543
@Import({ RestClientBuilderConfiguration.class, RestHighLevelClientConfiguration.class,
44+
RestClientConfiguration.class,
4645
RestClientSnifferConfiguration.class })
4746
public class ElasticsearchRestClientAutoConfiguration {
4847

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientConfigurations.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.springframework.beans.factory.ObjectProvider;
3838
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
3939
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
40+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
4041
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
4142
import org.springframework.boot.context.properties.PropertyMapper;
4243
import org.springframework.context.annotation.Bean;
@@ -112,14 +113,35 @@ private HttpHost createHttpHost(URI uri) {
112113
}
113114

114115
@Configuration(proxyBeanMethods = false)
115-
@ConditionalOnMissingBean(RestHighLevelClient.class)
116+
@ConditionalOnClass(RestHighLevelClient.class)
117+
@ConditionalOnMissingBean({
118+
RestHighLevelClient.class,
119+
RestClient.class
120+
})
116121
static class RestHighLevelClientConfiguration {
117122

118123
@Bean
119124
RestHighLevelClient elasticsearchRestHighLevelClient(RestClientBuilder restClientBuilder) {
120125
return new RestHighLevelClient(restClientBuilder);
121126
}
122127

128+
@Bean
129+
RestClient elasticsearchRestClient(RestHighLevelClient restHighLevelClient) {
130+
return restHighLevelClient.getLowLevelClient();
131+
}
132+
133+
}
134+
135+
@Configuration(proxyBeanMethods = false)
136+
@ConditionalOnMissingClass("org.elasticsearch.client.RestHighLevelClient")
137+
@ConditionalOnMissingBean(RestClient.class)
138+
static class RestClientConfiguration {
139+
140+
@Bean
141+
RestClient elasticsearchRestClient(RestClientBuilder restClientBuilder) {
142+
return restClientBuilder.build();
143+
}
144+
123145
}
124146

125147
@Configuration(proxyBeanMethods = false)

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientAutoConfigurationTests.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,21 @@
5555
* @author Brian Clozel
5656
* @author Vedran Pavic
5757
* @author Evgeniy Cheban
58+
* @author Filip Hrisafov
5859
*/
5960
class ElasticsearchRestClientAutoConfigurationTests {
6061

6162
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
6263
.withConfiguration(AutoConfigurations.of(ElasticsearchRestClientAutoConfiguration.class));
6364

6465
@Test
65-
void configureShouldOnlyCreateHighLevelRestClient() {
66-
this.contextRunner.run((context) -> assertThat(context).doesNotHaveBean(RestClient.class)
67-
.hasSingleBean(RestHighLevelClient.class));
66+
void configureShouldCreateHighLevelAndLowLevelRestClient() {
67+
this.contextRunner.run((context) -> {
68+
assertThat(context).hasSingleBean(RestClient.class)
69+
.hasSingleBean(RestHighLevelClient.class);
70+
assertThat(context.getBean(RestClient.class))
71+
.isEqualTo(context.getBean(RestHighLevelClient.class).getLowLevelClient());
72+
});
6873
}
6974

7075
@Test
@@ -74,10 +79,19 @@ void configureWhenCustomRestClientShouldBackOff() {
7479
.hasSingleBean(RestClient.class).hasBean("customRestClient"));
7580
}
7681

82+
@Test
83+
void configureWhenCustomRestHighLevelClientIsNotPresent() {
84+
this.contextRunner.withClassLoader(new FilteredClassLoader(RestHighLevelClient.class))
85+
.run((context) -> assertThat(context).doesNotHaveBean(RestHighLevelClient.class)
86+
.hasSingleBean(RestClient.class)
87+
.hasSingleBean(RestClientBuilder.class));
88+
}
89+
7790
@Test
7891
void configureWhenCustomRestHighLevelClientShouldBackOff() {
7992
this.contextRunner.withUserConfiguration(CustomRestHighLevelClientConfiguration.class)
80-
.run((context) -> assertThat(context).hasSingleBean(RestHighLevelClient.class));
93+
.run((context) -> assertThat(context).hasSingleBean(RestHighLevelClient.class)
94+
.doesNotHaveBean(RestClient.class));
8195
}
8296

8397
@Test

0 commit comments

Comments
 (0)