'elasticsearch java.lang.NoSuchMethodError: org.apache.http.client.utils.URLEncodedUtils.formatSegments

I'm trying this basic elasticsearch example from there guide to use java client API in a spring boot project.

but it gives me the following error when running:

java.lang.NoSuchMethodError: org.apache.http.client.utils.URLEncodedUtils.formatSegments([Ljava/lang/String;)Ljava/lang/String;

here is my POM file.

the code:

public void retrieveAuditMessages() throws IOException {
        // Create the low-level client
        RestClient restClient = RestClient.builder(
            new HttpHost("localhost", 9200)
        ).build();

    // Create the transport with a Jackson mapper
    ElasticsearchTransport transport = new RestClientTransport(
            restClient, new JacksonJsonpMapper()
    );

    // Create the API client
    ElasticsearchClient client = new ElasticsearchClient(transport);
    
    SearchResponse<String> search = client.search(s -> s
            .index("logstash-wildfly*")
            .query(q -> q
                    .term(t -> t
                            .field("host")
                            .value(v -> v.stringValue("aboSaadoosh"))
                    )
            ),
            String.class);

    for(Hit<String> hit: search.hits().hits())
    {
        System.out.println(hit.source());
    }
}

I guess it's a problem with dependencies, but I don't know how to solve it.

I'm using Elasticsearch version 8.1.1 java client API and spring boot 1.5.7.RELEASE



Solution 1:[1]

I solved this by adding org.apache.httpcomponents:httpclient:4.5.13 explicitly in the POM file (instead of version org.apache.httpcomponents:httpclient:4.5.3 included by co.elastic.clients:elasticsearch-java:8.1.1)

<dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpclient</artifactId>
  <version>4.5.13</version>
</dependency>

I don't know if this is the correct way to solve it but anyway it worked for me.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ibnSaadoosh