Calling Service method in component | Community
Skip to main content
February 26, 2016
Solved

Calling Service method in component

  • February 26, 2016
  • 8 replies
  • 3027 views

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.
Best answer by smacdonald2008

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
February 26, 2016

Please share the error. 

February 26, 2016

Have attached the error

edubey
February 26, 2016

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/

"
Jitendra_S_Toma
February 26, 2016

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

smacdonald2008
February 26, 2016

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

February 29, 2016

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

smacdonald2008
smacdonald2008Accepted solution
March 1, 2016

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. 

Jitendra_S_Toma
March 1, 2016

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

------

Jitendra