Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

Elasticsearch-java issue with sling model initiation AEM 6.5.16

Avatar

Level 2

I am trying to use elasticsearch-java rest api client within AEM servlet. As recommended on Elasticsearch site, I added maven dependencies. I can invoke sevlet properly. Adding elasticsearch-java dependency causes issue with my Sling model. We have sling models but it does not Inject resouce when elasticsearch-java is included in my pom.xml. Here is sample code for model

 

@Model( adaptables = Resource.class)
public class MyModelClass{

@inject
private Resource componentResource;

private String facetList;

 

@PostConstruct
protected void init() {
if(componentResource != null) {
// business logic 
} else {
LOGGER.error("Component node not found.");
}
}

 

I am not sure if issue is due to some maven dependency configuration or is incompatibility issue.  

AEM Version - 6.5.16

Elasticsearch-java - 8.11.3

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @abhijit_t 
Check the dependency tree of your project to identify any conflicting dependencies. You can use the Maven Dependency Plugin to generate a dependency tree. Run the following command in your project directory: mvn dependency:tree
Look for any conflicting versions of the same dependency. If you find any, you can try excluding the conflicting dependency from the Elasticsearch Java dependency in your pom.xml file.

Replace conflicting.dependency.groupId and conflicting-dependency-artifactId with the actual conflicting dependency details.

 

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-client</artifactId>
    <version>8.11.3</version>
    <exclusions>
        <exclusion>
            <groupId>conflicting.dependency.groupId</groupId>
            <artifactId>conflicting-dependency-artifactId</artifactId>
        </exclusion>
    </exclusions>
</dependency>

After excluding the conflicting dependency, rebuild your project and check if the resource injection issue is resolved.

Thanks.

 



View solution in original post

5 Replies

Avatar

Community Advisor

Perhaps the imports are mixed up; try using a more specific annotation to inject the resource. For example:

 

import org.apache.sling.models.annotations.injectorspecific.SlingObject;   

@Model(adaptables = Resource.class)
public class HelloWorldModel {

 @SlingObject
 private Resource currentResource;

 



Esteban Bustamante

Avatar

Correct answer by
Community Advisor

Hi @abhijit_t 
Check the dependency tree of your project to identify any conflicting dependencies. You can use the Maven Dependency Plugin to generate a dependency tree. Run the following command in your project directory: mvn dependency:tree
Look for any conflicting versions of the same dependency. If you find any, you can try excluding the conflicting dependency from the Elasticsearch Java dependency in your pom.xml file.

Replace conflicting.dependency.groupId and conflicting-dependency-artifactId with the actual conflicting dependency details.

 

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-client</artifactId>
    <version>8.11.3</version>
    <exclusions>
        <exclusion>
            <groupId>conflicting.dependency.groupId</groupId>
            <artifactId>conflicting-dependency-artifactId</artifactId>
        </exclusion>
    </exclusions>
</dependency>

After excluding the conflicting dependency, rebuild your project and check if the resource injection issue is resolved.

Thanks.

 



Avatar

Level 2

Hi @Raja_Reddy 

 

I tried finding conflicting dependencies but it did not gave any clue. I found a solution when I analyzed main elasticsearch-java pom.xml for dependencies. The elasticsearch-java is having dependency of 

 

<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>

 

I think this was creating problem with my sling model classes. When I marked these in exclusion in my project pom, it worked. My sling model and elasticsearch java api both worked properly. Many forums mentions issues about this.

 

I will be checking with Elastic team on possible impact  due to exclusion of jsr305.

 

Thanks

Abhijit

Avatar

Administrator

@abhijit_t Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.



Kautuk Sahni

Avatar

Level 2

In my project pom.xml, I excluded jsr305 and it worked for me.

 

<dependency>
      <groupId>co.elastic.clients</groupId>
      <artifactId>elasticsearch-java</artifactId>
      <version>8.11.3</version>
      <exclusions>
               <exclusion>
                     <groupId>commons-logging</groupId>
                     <artifactId>commons-logging</artifactId>
               </exclusion>
               <exclusion>
                      <groupId>com.google.code.findbugs</groupId>
                      <artifactId>jsr305</artifactId>
                </exclusion>
      </exclusions>
</dependency>