Skip to main content
Level 4
Feb 26, 2016 11:38 AM UTC
Solved

Calling Service method in component

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>

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
smacdonald2008
smacdonald2008Accepted solution
Level 10
Mar 1, 2016 12:08 AM UTC

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. 

8 replies

edubey
Level 10
Feb 26, 2016 12:28 PM UTC

Please share the error. 

Translate
Level 4
Feb 26, 2016 12:41 PM UTC

Have attached the error

Translate
edubey
Level 10
Feb 26, 2016 12:50 PM UTC

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/

"
Translate
Jitendra_S_Toma
Level 10
Feb 26, 2016 1:29 PM UTC

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

Translate
smacdonald2008
Level 10
Feb 26, 2016 1:53 PM UTC

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

Translate
Level 4
Feb 29, 2016 6:38 AM UTC

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

Translate
smacdonald2008
smacdonald2008Accepted solution
Level 10
Mar 1, 2016 12:08 AM UTC

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. 

Translate
Jitendra_S_Toma
Level 10
Mar 1, 2016 4:07 AM UTC

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

------

Jitendra

Translate