Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Calling Service method in component

Avatar

Level 4

I have created interface IContentService and implemented that interface in java class ContentService.java.I need to call the method of java class inside my component.

public interface IContentService {

public JSONObject getContent(String path) 
            throws ContentServiceExceptions,RepositoryException,IOException,JSONException;

}

 

 

The method in my java class returns the value in the form of JSON format.I want that JSON format data to be displayed through my component.I can able to display the JSON response by hitting the  service url,"http://localhost:4502/api/contentservice?pagetype=mspot&path=/content/pagename".But I want to display the JSON response by calling the service in my component.I have tried by calling the service by following it is throwing error,

<%
  
com.aaa.www.core.service.servlets.IContentService contentService = sling.getService(com.aaa.www.core.service.servlets.IContentService.class);

JSONObject ff = contentService.getContent(String path);
  
%>

<h3>The value of the key is: <%=ff%></h3>

1 Accepted Solution

Avatar

Correct answer by
Level 10

Assuming com.abc.www.core.service.servlets.IContentService is the interface - not the implementation class - this is proper syntax. Lets open a connect session - there is nothing more we can add this thread - you are doing it right.

The only way I can help you now is for you to send me your package and code. Then lets setup a connect session. 

View solution in original post

8 Replies

Avatar

Level 10

Can you attached complete log from error.log

and one more thing as I said previously

"

Why dont you go for servlet approach instead of OSGI service

Same way you can hit your servlet with parameter and get the JSON reponse

we do have alot of articles implemented this approach

Here's one: http://adobeaemclub.com/news-component-in-aem-using-guardian-news-api/

"

Avatar

Level 9

A simple way is to debug it. Check whether Your service is registered in OSGI console or not. Based on an error, it seems, IConentService is not registered.

--

Jitendra

Avatar

Level 10

Make sure your OSGi bundle is in active state and that your service can be seen under the service view. Look at this article: 

Creating your first AEM Service using an Adobe Maven Archetype project

Avatar

Level 4

Hi,

1. service is registered in OSGI console

2. bundle is active

3. I am getting error : 

An error occurred at line: 11 in the jsp file: /apps/petco/components/content/servicecomp/servicecomp.jsp JSONObject cannot be resolved to a type

my jsp:

<%
  String path=request.getParameter("path");
com.abc.www.core.service.servlets.IContentService contentService = sling.getService(com.abc.www.core.service.servlets.IContentService.class);
%>
<%
    JSONObject ob=contentService.getContent(path);

%>

- > I have also attached the error page

Avatar

Correct answer by
Level 10

Assuming com.abc.www.core.service.servlets.IContentService is the interface - not the implementation class - this is proper syntax. Lets open a connect session - there is nothing more we can add this thread - you are doing it right.

The only way I can help you now is for you to send me your package and code. Then lets setup a connect session. 

Avatar

Level 9

It seems ContentService implementation class has a getContent() method without argument and you are calling with the argument. Just check implementation code.

------

Jitendra