0

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

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Upgrading to a new version of Spring Boot might also be a good idea.
0

Whenever any dependency is added to pom.xml, the corresponding dependencies are also resolved. In this case, some maven dependency is using some other version of httpclient. That is why this conflict is arising. Please double check every added dependency and their corresponding dependencies.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.