diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/elasticsearch/ReactiveElasticsearchRestClientAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/elasticsearch/ReactiveElasticsearchRestClientAutoConfiguration.java index 753333c2c0..64c11abaf1 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/elasticsearch/ReactiveElasticsearchRestClientAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/elasticsearch/ReactiveElasticsearchRestClientAutoConfiguration.java @@ -74,6 +74,7 @@ public class ReactiveElasticsearchRestClientAutoConfiguration { .to((credentials) -> builder.withBasicAuth(credentials.getUsername(), credentials.getPassword())); map.from(this.properties.getConnectionTimeout()).to(builder::withConnectTimeout); map.from(this.properties.getSocketTimeout()).to(builder::withSocketTimeout); + map.from(this.properties.getPathPrefix()).to(builder::withPathPrefix); configureExchangeStrategies(map, builder); return builder.build(); } @@ -164,6 +165,10 @@ public class ReactiveElasticsearchRestClientAutoConfiguration { : this.restClientProperties.getMaxInMemorySize(); } + private String getPathPrefix() { + return this.deprecatedProperties.isCustomized() ? null : this.properties.getPathPrefix(); + } + private static final class Credentials { private final String username; diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchProperties.java index 738a793629..3810fedb82 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchProperties.java @@ -57,6 +57,11 @@ public class ElasticsearchProperties { */ private Duration socketTimeout = Duration.ofSeconds(30); + /** + * Prefix added to the path of every request sent to Elasticsearch. + */ + private String pathPrefix; + public List getUris() { return this.uris; } @@ -97,4 +102,12 @@ public class ElasticsearchProperties { this.socketTimeout = socketTimeout; } + public String getPathPrefix() { + return this.pathPrefix; + } + + public void setPathPrefix(String pathPrefix) { + this.pathPrefix = pathPrefix; + } + } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientConfigurations.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientConfigurations.java index e84a5c9cca..93076c4570 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientConfigurations.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientConfigurations.java @@ -80,6 +80,9 @@ class ElasticsearchRestClientConfigurations { builderCustomizers.orderedStream().forEach((customizer) -> customizer.customize(requestConfigBuilder)); return requestConfigBuilder; }); + if (this.properties.getPathPrefix() != null) { + builder.setPathPrefix(this.properties.properties.getPathPrefix()); + } builderCustomizers.orderedStream().forEach((customizer) -> customizer.customize(builder)); return builder; } @@ -253,6 +256,10 @@ class ElasticsearchRestClientConfigurations { : this.properties.getSocketTimeout(); } + private String getPathPrefix() { + return this.deprecatedProperties.isCustomized() ? null : this.properties.getPathPrefix(); + } + } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/elasticsearch/ReactiveElasticsearchRestClientAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/elasticsearch/ReactiveElasticsearchRestClientAutoConfigurationTests.java index f6180bafee..b0f0a7f31a 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/elasticsearch/ReactiveElasticsearchRestClientAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/elasticsearch/ReactiveElasticsearchRestClientAutoConfigurationTests.java @@ -256,6 +256,13 @@ class ReactiveElasticsearchRestClientAutoConfigurationTests { .isEqualTo(Duration.ofSeconds(2))); } + @Test + void whenPathPrefixIsConfiguredThenClientConfigurationHasPathPrefix() { + this.contextRunner.withPropertyValues("spring.elasticsearch.path-prefix=/some/prefix") + .run((context) -> assertThat(context.getBean(ClientConfiguration.class).getPathPrefix()) + .isEqualTo("/some/prefix")); + } + @ParameterizedPropertyPrefixTest void whenCredentialsAreConfiguredThenClientConfigurationHasDefaultAuthorizationHeader(String prefix) { this.contextRunner.withPropertyValues(prefix + "username=alice", prefix + "password=secret") diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientAutoConfigurationTests.java index fac35367e0..571e9cc9a2 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientAutoConfigurationTests.java @@ -201,6 +201,14 @@ class ElasticsearchRestClientAutoConfigurationTests { }); } + @Test + void configureWithCustomPathPrefix() { + this.contextRunner.withPropertyValues("spring.elasticsearch.path-prefix=/some/prefix").run((context) -> { + RestClient client = context.getBean(RestHighLevelClient.class).getLowLevelClient(); + assertThat(client).extracting("pathPrefix").isEqualTo("/some/prefix"); + }); + } + @Test void configureWithoutSnifferLibraryShouldNotCreateSniffer() { this.contextRunner.withClassLoader(new FilteredClassLoader("org.elasticsearch.client.sniff"))