Elasticsearch-java issue with sling model initiation AEM 6.5.16 | Community
Skip to main content
Level 2
January 15, 2024
Solved

Elasticsearch-java issue with sling model initiation AEM 6.5.16

  • January 15, 2024
  • 3 replies
  • 1083 views

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{

@586265
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

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Raja_Reddy

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.

 

3 replies

EstebanBustamante
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
January 15, 2024

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
Raja_Reddy
Community Advisor
Raja_ReddyCommunity AdvisorAccepted solution
Community Advisor
January 16, 2024

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.

 

abhijit_tAuthor
Level 2
January 16, 2024

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

kautuk_sahni
Community Manager
Community Manager
January 16, 2024

@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
abhijit_tAuthor
Level 2
January 16, 2024

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>