【问题标题】:Elasticsearch RestHighLevelClient missing transitive dependenciesElasticsearch RestHighLevelClient 缺少传递依赖项
【发布时间】:2022-04-01 18:53:18
【问题描述】:

我正在尝试通过此依赖项使用 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)

似乎缺少大量传递依赖项


更新

原来这里发生了两件事。

  1. 正如@miroh 在下面指出的那样,我需要明确定义org.elasticsearch:elasticserach:6.0.1 依赖项,这样我就不会得到错误的版本。不知道为什么这是必要的,但似乎确实是必要的...... (也在这里引用:https://github.com/elastic/elasticsearch/issues/26959

  2. 我的项目是一个具有许多其他依赖项的多模块 Spring 项目。显然,一些 Spring/Springboot 依赖项依赖于 spring-data-elasticsearch,后者依赖于 org.elasticsearch:elasticsearch:2.4.6。我通过将 elasticsearch:6.0.1 版本添加到我的父 pom.xml 的依赖管理部分来解决了这些冲突的 elasticsearch 版本。当存在依赖冲突时,这告诉 maven 使用 6.0.1 版本。

【问题讨论】:

    标签: java maven elasticsearch dependencies elasticsearch-java-api


    【解决方案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>
    

    【讨论】:

    • 感谢@miroh 这有效。我实际上只需要明确定义 org.elasticsearch:elasticsearh 而不是 elasticserach-rest-client 依赖项。不知道为什么 elasticsearch-rest-high-level-client 似乎无法引入它自己的依赖项。
    • @Fabian 这是编译时间与运行时依赖关系的问题。当您构建代码时,如果您不使用这些依赖项,它将毫无问题地编译。但是在运行时,如果它们被预编译的部分使用,则会发生这些类型的错误。如果您打算使用其他高级客户端方法,您可能会遇到类似的错误,因为 rest-client 不在运行时类路径中。要轻松解决这些类型的问题,请始终检查 pom.xml(或 build.gradle)文件
    • @Fabian 例如这是高级客户 gradle 文件 github.com/elastic/elasticsearch/blob/master/client/…
    【解决方案2】:

    添加

    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>elasticsearch-rest-client</artifactId>
        <version>${es.version}</version>
    </dependency>
    

    【讨论】:

      猜你喜欢
      • 2011-04-10
      • 1970-01-01
      • 2017-12-25
      • 2015-04-18
      • 2020-03-24
      • 2012-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多