Using ElasticSearch Java API on an EAR project in Glassfish 4.1

Hello,

I'm trying to create a EAR application that talks to ElasticSearch. But I can't get it to work. It keeps complaining about classes not being able to load.

I know Glassfish has it's own implementations of Jackson, so I tried to use the libraries attribute while deploying the app. Doesn't solve it 100%. Now I have a "dependencies" project which has jackson and ElasticSearch as dependencies, and I use the maven shade plugin like explained on the documentation. When I place that jar in the /domain/lib folder and try to run the application, I still get the following exception:

Caused by: java.lang.NoSuchFieldError: FAIL_ON_SYMBOL_HASH_OVERFLOW
at org.elasticsearch.common.xcontent.smile.SmileXContent.(SmileXContent.java:46)
at org.elasticsearch.common.xcontent.XContentFactory.contentBuilder(XContentFactory.java:124)
at org.elasticsearch.action.support.ToXContentToBytes.buildAsBytes(ToXContentToBytes.java:62)
at org.elasticsearch.action.search.SearchRequest.source(SearchRequest.java:250)
at org.elasticsearch.action.search.SearchRequestBuilder.beforeExecute(SearchRequestBuilder.java:1027)
at org.elasticsearch.action.search.SearchRequestBuilder.beforeExecute(SearchRequestBuilder.java:50)
at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:85)
at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:59)
at com.example.ESMessagesSessionBean.getMessages(ESMessagesSessionBean.java:23)

These are the libraries I include in the "dependencies" project

<dependencies>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.6.2</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-core-asl</artifactId>
        <version>1.9.4</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.6.0</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.6.1</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.dataformat</groupId>
        <artifactId>jackson-dataformat-cbor</artifactId>
        <version>2.6.2</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.dataformat</groupId>
        <artifactId>jackson-dataformat-smile</artifactId>
        <version>2.6.2</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.dataformat</groupId>
        <artifactId>jackson-dataformat-yaml</artifactId>
        <version>2.6.2</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-joda</artifactId>
        <version>2.6.1</version>
    </dependency>
    
    <dependency>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch</artifactId>
        <version>2.2.1</version>
        <scope>compile</scope>
    </dependency>
</dependencies>`

Any idea on how to fix this issue? I ran out of ideas.

Erates

Hi,

the problem is, that Glassfish 4.1 provides the Jackson libraries as OSGI bundle. You aaplication uses jackson libraries from the OSGI cache, even if you do not develop an OSGI application.

The version of Jackson, glassfish 4.1 provides, is 2.3.2 and this version differs from the version, elasticsearch Java API is using. The older Version does not have the field FAIL_ON_SYMBOL_HASH_OVERFLOW in com.fasterxml.jackson.core.JsonFactory$Feature.

THe solution is to update the version of Jackson in glassfish 4.1. The way you should do this, is explained here: Stackoverflow.

If you are using elasticsearch 2.3.2, you also have to update the version of guava. elasticsearch depends on guava-18.0 and glassfish provides an older version.

Hope, it helps...

++

You might want to shade elasticsearch deps if you wish...

Read https://www.elastic.co/blog/to-shade-or-not-to-shade in case it helps...