4

I'm trying to use the RestHighLevelClient via this dependency

    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>elasticsearch-rest-high-level-client</artifactId>
        <version>6.0.1</version>
    </dependency>

But I keep getting ClassNotFoundExceptions in the RestHighLevelClient class.

When I try to wire up this 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);
}

I receive these errors:

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)

It looks like tons of the transitive dependencies are missing enter image description here


Update

Turns out there were two things going on here.

  1. As @miroh pointed out below I need to explicitly define the org.elasticsearch:elasticserach:6.0.1 dependency so I don't get the wrong version pulled in. Not sure why this is necessary but it seems to indeed be necessary ... (also referenced here : https://github.com/elastic/elasticsearch/issues/26959)

  2. My project is a multi module Spring project with many other dependencies. Apparently, some of the Spring/Springboot dependencies were dependent on spring-data-elasticsearch which has a dependency on org.elasticsearch:elasticsearch:2.4.6. I resolved these conflicting versions of elasticsearch by adding the elasticsearch:6.0.1 version to the dependency management section of my parent pom.xml. This tells maven to use version 6.0.1 when dependency conflicts are present.

Fabian
  • 3,310
  • 3
  • 26
  • 35

2 Answers2

12

Try adding following dependencies.

<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>
miskender
  • 7,460
  • 1
  • 19
  • 23
  • Thanks @miroh this worked. I actually only needed to explicitly define the org.elasticsearch:elasticsearh and not the elasticserach-rest-client dependency. Not sure why the elasticsearch-rest-high-level-client seems incapable of pulling in it's own dependencies. – Fabian Feb 27 '18 at 17:58
  • 1
    @Fabian It's compile time vs runtime dependency issue. When you build your code and If you dont use those dependencies It will compile without any problem. But in the runtime if they are used by precomplied parts these types of errors occur. If you are going to use other methods of high-level client, you probably get similar errors because rest-client is not in runtime classpath. To easly solve these types of problems always check pom.xml (or build.gradle) files – miskender Feb 27 '18 at 18:08
  • @Fabian for example this is high-level-clients gradle file https://github.com/elastic/elasticsearch/blob/master/client/rest-high-level/build.gradle – miskender Feb 27 '18 at 18:09
0

Add

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-client</artifactId>
    <version>${es.version}</version>
</dependency>
Rajiv Singh
  • 958
  • 1
  • 9
  • 14