Elasticsearch - Low Level Rest Client Initialisation throwing exception

Hi,
I am working on implementing Low Level Rest Client.
I am facing issues with Initialising RestClient.
The dependency in pom.xml is :

  <dependency>
		    <groupId>org.elasticsearch.client</groupId>
		    <artifactId>elasticsearch-rest-client</artifactId>
		    <version>6.4.3</version>
		</dependency>

Initialisation of RestClient is :

RestClient  restClient =RestClient.builder( new HttpHost("10.50.1.100", 9998, "http")).build();

This particular line of code is throwing an ClassNotFoundException
To be specific -

SEVERE: Servlet.service() for servlet [elasticsearch] in context with path [/ElasticSearch] threw exception [Handler processing failed; nested exception is java.lang.NoClassDefFoundError: org/apache/http/conn/util/PublicSuffixMatcherLoader] with root cause
java.lang.ClassNotFoundException: org.apache.http.conn.util.PublicSuffixMatcherLoader
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1285)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1119)
at org.apache.http.impl.nio.client.HttpAsyncClientBuilder.build(HttpAsyncClientBuilder.java:640)
at org.elasticsearch.client.RestClientBuilder$2.run(RestClientBuilder.java:230)
at org.elasticsearch.client.RestClientBuilder$2.run(RestClientBuilder.java:227)
at java.security.AccessController.doPrivileged(Native Method)
at org.elasticsearch.client.RestClientBuilder.createHttpClient(RestClientBuilder.java:227)
at org.elasticsearch.client.RestClientBuilder.access$000(RestClientBuilder.java:42)
at org.elasticsearch.client.RestClientBuilder$1.run(RestClientBuilder.java:198)
at org.elasticsearch.client.RestClientBuilder$1.run(RestClientBuilder.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at org.elasticsearch.client.RestClientBuilder.build(RestClientBuilder.java:195)

I am sure I am missing something very basic here.
Where am I going wrong?

Sounds like you are running your code in a WebApp.
May be you have some dependencies which are clashing with the ones used by the Rest Client. (Http Client).

2 Likes

Thanks for your response
As I see, I have the following http Client dependency on my application

<!-- Apache Http client -->
<dependency>
	<groupId>org.apache.httpcomponents</groupId>
	<artifactId>httpclient</artifactId>
	<version>4.3.5</version>
</dependency>
<dependency>
	<groupId>org.apache.httpcomponents</groupId>
	<artifactId>httpcore</artifactId>
	<version>4.3.2</version>
</dependency>

After removing these dependencies, my code started to work fine.

Although I have one doubt, Is it a safe practice to initialise Rest Client each time I do some operation like create index or search request and close it right after its usage? Or is it okay to initialise Rest Client once at the server start up ?

I'd use a singleton and initialize it once per application.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.