javaspring-bootopensearchamazon-opensearchspring-boot-3

Opensearch Springboot : When starting App getting Jackson Error


In my local I am running opensearch 2.12 using docker with security disabled.

Using springboot 3, Opensearch starter 1.3, opensearch-rest-high-level-client 2.12 When running my springboot app, I am running into this error. I am just trying to connect to opensearch and create an index -

2024-03-16T19:11:57.033+05:30  INFO 23080 --- [           main] c.e.e.e.ElasticsearchPocApplication      : No active profile set, falling back to 1 default profile: "default"
2024-03-16T19:11:57.298+05:30  INFO 23080 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Elasticsearch repositories in DEFAULT mode.
2024-03-16T19:11:57.308+05:30  INFO 23080 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 8 ms. Found 0 Elasticsearch repository interfaces.
2024-03-16T19:11:57.496+05:30  INFO 23080 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port 8080 (http)
2024-03-16T19:11:57.501+05:30  INFO 23080 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2024-03-16T19:11:57.501+05:30  INFO 23080 --- [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.19]
2024-03-16T19:11:57.532+05:30  INFO 23080 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2024-03-16T19:11:57.533+05:30  INFO 23080 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 475 ms
2024-03-16T19:11:57.919+05:30  INFO 23080 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port 8080 (http) with context path ''
2024-03-16T19:11:57.923+05:30  INFO 23080 --- [           main] c.e.e.e.ElasticsearchPocApplication      : Started ElasticsearchPocApplication in 1.073 seconds (process running for 1.253)
Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/StreamWriteConstraints
    at org.opensearch.common.xcontent.json.JsonXContent.<clinit>(JsonXContent.java:82)
    at org.opensearch.common.xcontent.XContentType$1.contentBuilder(XContentType.java:94)
    at org.opensearch.core.xcontent.MediaTypeRegistry.contentBuilder(MediaTypeRegistry.java:134)
    at org.opensearch.client.indices.CreateIndexRequest.mapping(CreateIndexRequest.java:189)
    at com.example.elasticsearch.elasticsearchpoc.ElasticsearchPocApplication.main(ElasticsearchPocApplication.java:57)
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.core.StreamWriteConstraints
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
    ... 5 more

This is my POM -

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example.elasticsearch</groupId>
    <artifactId>elasticsearch-poc</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>elasticsearch-poc</name>
    <description>Demo project for Spring Boot ElasticSearch</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test-autoconfigure</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.opensearch.client</groupId>
            <artifactId>spring-data-opensearch-starter</artifactId>
            <version>1.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.opensearch.client</groupId>
            <artifactId>spring-data-opensearch-test-autoconfigure</artifactId>
            <version>1.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.opensearch.client</groupId>
            <artifactId>opensearch-rest-high-level-client</artifactId>
            <version>2.12.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>


This is my code -

package com.example.elasticsearch.elasticsearchpoc;

import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.opensearch.client.RequestOptions;
import org.opensearch.client.RestClient;
import org.opensearch.client.RestClientBuilder;
import org.opensearch.client.RestHighLevelClient;
import org.opensearch.client.indices.CreateIndexRequest;
import org.opensearch.client.indices.CreateIndexResponse;
import org.opensearch.common.settings.Settings;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration;

import java.io.IOException;
import java.util.HashMap;

@SpringBootApplication(exclude = {ElasticsearchDataAutoConfiguration.class})
public class ElasticsearchPocApplication {

    public static void main(String[] args) throws IOException {

        SpringApplication.run(ElasticsearchPocApplication.class, args);

        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();

        credentialsProvider.setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials("admin", "admin"));

        RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200, "http"))
                .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                    @Override
                    public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                        return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
                    }
                });
        RestHighLevelClient client = new RestHighLevelClient(builder);

        CreateIndexRequest createIndexRequest = new CreateIndexRequest("custom-index");

        createIndexRequest.settings(Settings.builder() //Specify in the settings how many shards you want in the index.
                .put("index.number_of_shards", 4)
                .put("index.number_of_replicas", 3)
        );
        //Create a set of maps for the index's mappings.
        HashMap<String, String> typeMapping = new HashMap<String,String>();
        typeMapping.put("type", "integer");
        HashMap<String, Object> ageMapping = new HashMap<String, Object>();
        ageMapping.put("age", typeMapping);
        HashMap<String, Object> mapping = new HashMap<String, Object>();
        mapping.put("properties", ageMapping);
        createIndexRequest.mapping(mapping);
        CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT);

    }

}



EDIT 1

mvn dependency:tree shows only jackson 2.15.x

--- dependency:3.6.1:tree (default-cli) @ elasticsearch-poc ---
[INFO] com.example.elasticsearch:elasticsearch-poc:jar:0.0.1-SNAPSHOT
[INFO] +- org.springframework.boot:spring-boot-test-autoconfigure:jar:3.2.3:runtime
[INFO] |  +- org.springframework.boot:spring-boot:jar:3.2.3:compile
[INFO] |  |  \- org.springframework:spring-context:jar:6.1.4:compile
[INFO] |  +- org.springframework.boot:spring-boot-test:jar:3.2.3:runtime
[INFO] |  \- org.springframework.boot:spring-boot-autoconfigure:jar:3.2.3:compile
[INFO] +- org.opensearch.client:spring-data-opensearch-starter:jar:1.3.0:compile
[INFO] |  +- org.opensearch.client:spring-data-opensearch:jar:1.3.0:compile
[INFO] |  |  +- org.springframework.data:spring-data-commons:jar:3.2.3:compile
[INFO] |  |  +- org.springframework.data:spring-data-elasticsearch:jar:5.2.3:compile
[INFO] |  |  +- com.fasterxml.jackson.core:jackson-core:jar:2.15.4:compile
[INFO] |  |  +- com.fasterxml.jackson.core:jackson-databind:jar:2.15.4:compile
[INFO] |  |  |  \- com.fasterxml.jackson.core:jackson-annotations:jar:2.15.4:compile
[INFO] |  |  \- org.springframework:spring-tx:jar:6.1.4:compile
[INFO] |  +- org.opensearch.client:opensearch-rest-client:jar:2.11.0:compile
[INFO] |  |  +- org.apache.httpcomponents:httpclient:jar:4.5.14:compile
[INFO] |  |  +- org.apache.httpcomponents:httpcore:jar:4.4.16:compile
[INFO] |  |  +- org.apache.httpcomponents:httpasyncclient:jar:4.1.5:compile
[INFO] |  |  +- org.apache.httpcomponents:httpcore-nio:jar:4.4.16:compile
[INFO] |  |  \- commons-codec:commons-codec:jar:1.16.1:compile
[INFO] |  \- org.opensearch.client:opensearch-rest-client-sniffer:jar:2.11.0:runtime
[INFO] +- org.opensearch.client:spring-data-opensearch-test-autoconfigure:jar:1.3.0:compile
[INFO] |  +- org.junit.jupiter:junit-jupiter:jar:5.10.2:runtime
[INFO] |  |  +- org.junit.jupiter:junit-jupiter-api:jar:5.10.2:runtime
[INFO] |  |  |  +- org.opentest4j:opentest4j:jar:1.3.0:runtime
[INFO] |  |  |  +- org.junit.platform:junit-platform-commons:jar:1.10.2:runtime
[INFO] |  |  |  \- org.apiguardian:apiguardian-api:jar:1.1.2:runtime
[INFO] |  |  +- org.junit.jupiter:junit-jupiter-params:jar:5.10.2:runtime
[INFO] |  |  \- org.junit.jupiter:junit-jupiter-engine:jar:5.10.2:runtime
[INFO] |  |     \- org.junit.platform:junit-platform-engine:jar:1.10.2:runtime
[INFO] |  \- org.springframework:spring-test:jar:6.1.4:runtime
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:3.2.3:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter:jar:3.2.3:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-starter-logging:jar:3.2.3:compile
[INFO] |  |  |  +- ch.qos.logback:logback-classic:jar:1.4.14:compile
[INFO] |  |  |  |  \- ch.qos.logback:logback-core:jar:1.4.14:compile
[INFO] |  |  |  +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.21.1:compile
[INFO] |  |  |  \- org.slf4j:jul-to-slf4j:jar:2.0.12:compile
[INFO] |  |  +- jakarta.annotation:jakarta.annotation-api:jar:2.1.1:compile
[INFO] |  |  \- org.yaml:snakeyaml:jar:2.2:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-json:jar:3.2.3:compile
[INFO] |  |  +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.15.4:compile
[INFO] |  |  +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.15.4:compile
[INFO] |  |  \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.15.4:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-tomcat:jar:3.2.3:compile
[INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-core:jar:10.1.19:compile
[INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-el:jar:10.1.19:compile
[INFO] |  |  \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:10.1.19:compile
[INFO] |  +- org.springframework:spring-web:jar:6.1.4:compile
[INFO] |  |  +- org.springframework:spring-beans:jar:6.1.4:compile
[INFO] |  |  \- io.micrometer:micrometer-observation:jar:1.12.3:compile
[INFO] |  |     \- io.micrometer:micrometer-commons:jar:1.12.3:compile
[INFO] |  \- org.springframework:spring-webmvc:jar:6.1.4:compile
[INFO] |     +- org.springframework:spring-aop:jar:6.1.4:compile
[INFO] |     \- org.springframework:spring-expression:jar:6.1.4:compile
[INFO] +- org.springframework.boot:spring-boot-starter-test:jar:3.2.3:test
[INFO] |  +- com.jayway.jsonpath:json-path:jar:2.9.0:test
[INFO] |  |  \- org.slf4j:slf4j-api:jar:2.0.12:compile
[INFO] |  +- jakarta.xml.bind:jakarta.xml.bind-api:jar:4.0.1:test
[INFO] |  |  \- jakarta.activation:jakarta.activation-api:jar:2.1.2:test
[INFO] |  +- net.minidev:json-smart:jar:2.5.0:test
[INFO] |  |  \- net.minidev:accessors-smart:jar:2.5.0:test
[INFO] |  |     \- org.ow2.asm:asm:jar:9.3:test
[INFO] |  +- org.assertj:assertj-core:jar:3.24.2:test
[INFO] |  |  \- net.bytebuddy:byte-buddy:jar:1.14.12:test
[INFO] |  +- org.awaitility:awaitility:jar:4.2.0:test
[INFO] |  +- org.hamcrest:hamcrest:jar:2.2:test
[INFO] |  +- org.mockito:mockito-core:jar:5.7.0:test
[INFO] |  |  +- net.bytebuddy:byte-buddy-agent:jar:1.14.12:test
[INFO] |  |  \- org.objenesis:objenesis:jar:3.3:test
[INFO] |  +- org.mockito:mockito-junit-jupiter:jar:5.7.0:test
[INFO] |  +- org.skyscreamer:jsonassert:jar:1.5.1:test
[INFO] |  |  \- com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:test
[INFO] |  +- org.springframework:spring-core:jar:6.1.4:compile
[INFO] |  |  \- org.springframework:spring-jcl:jar:6.1.4:compile
[INFO] |  \- org.xmlunit:xmlunit-core:jar:2.9.1:test
[INFO] \- org.opensearch.client:opensearch-rest-high-level-client:jar:2.12.0:compile
[INFO]    +- org.opensearch:opensearch:jar:2.12.0:compile
[INFO]    |  +- org.opensearch:opensearch-common:jar:2.12.0:compile
[INFO]    |  +- org.opensearch:opensearch-core:jar:2.12.0:compile
[INFO]    |  +- org.opensearch:opensearch-compress:jar:2.12.0:compile
[INFO]    |  |  \- com.github.luben:zstd-jni:jar:1.5.5-5:compile
[INFO]    |  +- org.opensearch:opensearch-secure-sm:jar:2.12.0:compile
[INFO]    |  +- org.opensearch:opensearch-x-content:jar:2.12.0:compile
[INFO]    |  |  +- com.fasterxml.jackson.dataformat:jackson-dataformat-smile:jar:2.15.4:compile
[INFO]    |  |  +- com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:jar:2.15.4:compile
[INFO]    |  |  \- com.fasterxml.jackson.dataformat:jackson-dataformat-cbor:jar:2.15.4:compile
[INFO]    |  +- org.opensearch:opensearch-geo:jar:2.12.0:compile
[INFO]    |  +- org.opensearch:opensearch-telemetry:jar:2.12.0:compile
[INFO]    |  +- org.apache.lucene:lucene-core:jar:9.9.2:compile
[INFO]    |  +- org.apache.lucene:lucene-analysis-common:jar:9.9.2:compile
[INFO]    |  +- org.apache.lucene:lucene-backward-codecs:jar:9.9.2:compile
[INFO]    |  +- org.apache.lucene:lucene-grouping:jar:9.9.2:compile
[INFO]    |  +- org.apache.lucene:lucene-highlighter:jar:9.9.2:compile
[INFO]    |  +- org.apache.lucene:lucene-join:jar:9.9.2:compile
[INFO]    |  +- org.apache.lucene:lucene-memory:jar:9.9.2:compile
[INFO]    |  +- org.apache.lucene:lucene-misc:jar:9.9.2:compile
[INFO]    |  +- org.apache.lucene:lucene-queries:jar:9.9.2:compile
[INFO]    |  +- org.apache.lucene:lucene-queryparser:jar:9.9.2:compile
[INFO]    |  +- org.apache.lucene:lucene-sandbox:jar:9.9.2:compile
[INFO]    |  +- org.apache.lucene:lucene-spatial-extras:jar:9.9.2:compile
[INFO]    |  +- org.apache.lucene:lucene-spatial3d:jar:9.9.2:compile
[INFO]    |  +- org.apache.lucene:lucene-suggest:jar:9.9.2:compile
[INFO]    |  +- org.opensearch:opensearch-cli:jar:2.12.0:compile
[INFO]    |  |  \- net.sf.jopt-simple:jopt-simple:jar:5.0.4:compile
[INFO]    |  +- joda-time:joda-time:jar:2.12.2:compile
[INFO]    |  +- com.tdunning:t-digest:jar:3.2:compile
[INFO]    |  +- org.hdrhistogram:HdrHistogram:jar:2.1.12:compile
[INFO]    |  +- org.apache.logging.log4j:log4j-api:jar:2.21.1:compile
[INFO]    |  +- org.apache.logging.log4j:log4j-jul:jar:2.21.1:compile
[INFO]    |  +- net.java.dev.jna:jna:jar:5.13.0:compile
[INFO]    |  +- com.jcraft:jzlib:jar:1.1.3:compile
[INFO]    |  +- io.projectreactor:reactor-core:jar:3.6.3:compile
[INFO]    |  +- org.reactivestreams:reactive-streams:jar:1.0.4:compile
[INFO]    |  \- com.google.protobuf:protobuf-java:jar:3.22.3:compile
[INFO]    +- org.opensearch.plugin:mapper-extras-client:jar:2.12.0:compile
[INFO]    +- org.opensearch.plugin:parent-join-client:jar:2.12.0:compile
[INFO]    +- org.opensearch.plugin:aggs-matrix-stats-client:jar:2.12.0:compile
[INFO]    +- org.opensearch.plugin:rank-eval-client:jar:2.12.0:compile
[INFO]    \- org.opensearch.plugin:lang-mustache-client:jar:2.12.0:compile
[INFO]       \- com.github.spullara.mustache.java:compiler:jar:0.9.10:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------


Solution

  • This is error is gone after I used this dependency. This is the latest version at the time of writing this answer

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.17.0</version>
    </dependency>