我正在尝试通过此依赖项使用 RestHighLevelClient
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.0.1</version>
</dependency>
但我一直在 RestHighLevelClient
类中收到 ClassNotFoundExceptions。
当我尝试连接这个 bean 时(AwsAmsElasticsearchClientConfig.java
:
@Bean(name = "elasticsearchRestClient")
public RestHighLevelClient getElasticsearchRestClient() {
RestClientBuilder restClientBuilder = RestClient.builder(
new HttpHost(HOST, PORT, SCHEME));
restClientBuilder.setHttpClientConfigCallback(
new RestClientBuilder.HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
httpClientBuilder.addInterceptorLast(buildAwsSigningRequestInterceptor());
// add SSL config if needed
// httpClientBuilder.setSSLContext(null);
return httpClientBuilder;
}
}
);
return new RestHighLevelClient(restClientBuilder);
}
我收到这些错误:
19:51:11.026 ERROR [ main] SpringApplication - Application startup failed
java.lang.ClassNotFoundException: org.elasticsearch.common.CheckedConsumer
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357) [1 skipped]
at com.glassdoor.applicantManagement.aws.AwsAmsElasticsearchClientConfig.getElasticsearchRestClient(AwsAmsElasticsearchClientConfig.java:63) [2 skipped]
at com.glassdoor.applicantManagement.aws.AwsAmsElasticsearchClientConfig$$EnhancerBySpringCGLIB$$c8f9ed07.CGLIB$getElasticsearchRestClient$0(<generated>)
Wrapped by: java.lang.NoClassDefFoundError: org/elasticsearch/common/CheckedConsumer
at com.glassdoor.applicantManagement.aws.AwsAmsElasticsearchClientConfig.getElasticsearchRestClient(AwsAmsElasticsearchClientConfig.java:63) [2 skipped]
at com.glassdoor.applicantManagement.aws.AwsAmsElasticsearchClientConfig$$EnhancerBySpringCGLIB$$c8f9ed07.CGLIB$getElasticsearchRestClient$0(<generated>)
at com.glassdoor.applicantManagement.aws.AwsAmsElasticsearchClientConfig$$EnhancerBySpringCGLIB$$c8f9ed07$$FastClassBySpringCGLIB$$fba1a4f2.invoke(<generated>)
at com.glassdoor.applicantManagement.aws.AwsAmsElasticsearchClientConfig$$EnhancerBySpringCGLIB$$c8f9ed07.getElasticsearchRestClient(<generated>) [2 skipped]
at java.lang.reflect.Method.invoke(Method.java:498) [3 skipped]
Wrapped by: java.lang.BootstrapMethodError: java.lang.NoClassDefFoundError: org/elasticsearch/common/CheckedConsumer
at com.glassdoor.applicantManagement.aws.AwsAmsElasticsearchClientConfig.getElasticsearchRestClient(AwsAmsElasticsearchClientConfig.java:63) [2 skipped]
at com.glassdoor.applicantManagement.aws.AwsAmsElasticsearchClientConfig$$EnhancerBySpringCGLIB$$c8f9ed07.CGLIB$getElasticsearchRestClient$0(<generated>)
at com.glassdoor.applicantManagement.aws.AwsAmsElasticsearchClientConfig$$EnhancerBySpringCGLIB$$c8f9ed07$$FastClassBySpringCGLIB$$fba1a4f2.invoke(<generated>)
at com.glassdoor.applicantManagement.aws.AwsAmsElasticsearchClientConfig$$EnhancerBySpringCGLIB$$c8f9ed07.getElasticsearchRestClient(<generated>) [2 skipped]
at java.lang.reflect.Method.invoke(Method.java:498) [3 skipped]
Wrapped by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.elasticsearch.client.RestHighLevelClient]: Factory method 'getElasticsearchRestClient' threw exception; nested exception is java.lang.BootstrapMethodError: java.lang.NoClassDefFoundError: org/elasticsearch/common/CheckedConsumer
at io.undertow.servlet.core.DeploymentManagerImpl$1.call(DeploymentManagerImpl.java:192) [148 skipped]
at io.undertow.servlet.core.DeploymentManagerImpl$1.call(DeploymentManagerImpl.java:174)
at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:42)
at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
更新
原来这里发生了两件事。
正如@miroh 在下面指出的那样,我需要明确定义
org.elasticsearch:elasticserach:6.0.1
依赖项,这样我就不会引入错误的版本。不知道为什么这是必要的,但似乎确实是必要的……(此处也引用:https://github.com/elastic/elasticsearch/issues/26959)我的项目是一个具有许多其他依赖项的多模块 Spring 项目。显然,某些 Spring/Springboot 依赖项依赖于
spring-data-elasticsearch
,后者依赖于org.elasticsearch:elasticsearch:2.4.6
。我通过将 elasticsearch:6.0.1 版本添加到父级pom.xml
的依赖管理部分来解决这些冲突的 elasticsearch 版本。这告诉 Maven 在存在依赖冲突时使用版本 6.0.1。
最佳答案
尝试添加以下依赖项。
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.0.1</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>6.0.1</version>
</dependency>
关于java - Elasticsearch RestHighLevelClient 缺少传递依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48959212/