Calling Service in component | Community
Skip to main content
varuns46785756
Level 5
February 26, 2016
Solved

Calling Service in component

  • February 26, 2016
  • 16 replies
  • 5467 views

Hi,

I am trying to call Service from my component and my service return json data, but when I am trying to call the service from Component , I am getting error.Please let me know how can I call the json data in my component jsp.

Please find attached my interface and implementation class.I am trying to call :

<%
com.petco.www.core.service.servlets.IContentService servicedata = sling.getService(com.petco.www.core.service.servlets.IContentService.class);
%>

<%=servicedata.getContent(String path)%>

CODE:

package com.abc.www.core.service.servlets;

import java.io.IOException;

import javax.jcr.RepositoryException;

import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;

import com.petco.www.core.service.exceptions.ContentServiceExceptions;

/*
 * This is an interface class to retrieve content from the AEM Services. This interface will be invoked by different channel 
 * to get content using AEM services
 * 
 
 */

public interface IContentService {
    
    /**
     * This method will be invoked to get the content from AEM services.
     * 
     * @param pageType
     * @param path
     * @return String
     */
    public JSONObject getContent(String path) 
            throws ContentServiceExceptions,RepositoryException,IOException,JSONException;
}

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 Jitendra_S_Toma

iNTERFACE HAS METHOD WITH STRING.

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

JSONObject finalJsonObjects = servicedata.getContent(); // ARGUMENT IS MISSING HERE. YOUR SHOULD PASS SOMETHING HERE.

%>

16 replies

Jitendra_S_Toma
Level 10
February 26, 2016

A few follow-up questions:

  • What kind of error?.
  • I believe you have implementation class as well. Have you verified the annotations of implementation class?. A small code snippet could help us.

--

Jitendra

varuns46785756
Level 5
February 26, 2016

Hi Jitendra,

Please find attached  Implementation class, and I am fetching the jcr node properties in json format , I am using service url for hitting the page,like:" http://localhost:4502/api/contentservice?pagetype=mspot&path=/content/pagename" and I am getting the json data correctly.

Now I need to call this code from component and display the json data in html format on browser.For this , I have created a component and I am trying to call the data in component like dis:

<%

com.abc.www.core.service.servlets.IContentService servicedata = sling.getService(com.abc.www.core.service.servlets.IContentService.class);

JSONObject finalJsonObjects = servicedata.getContent();

%>

I am not able to call the json object here.Please let me know the correct way to call the json data here.

 

This is my Implementation class: ContentService.java

 
edubey
Level 10
February 26, 2016

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/

edubey
Level 10
February 26, 2016

Make sure :

1. your service is registered in OSGI console

2. bundle is active

3. are you seeing any error like  "only type can be imported resolves to a package" in error.log

Is this your thread :

http://help-forums.adobe.com/content/adobeforums/en/experience-manager-forum/adobe-experience-manager.topic.html/forum__5cre-i_have_createdinter.html 

smacdonald2008
Level 10
February 26, 2016

You are calling it the correct way - sling.getService().

If that is not found (and bunlde is active)- you may have a package conflict. 

If you have a lot of services in the same package - i find AEM sometimes has issues. Try placing your service in a new package for testing purposes - ie - foo.mypackage. 

Let us know what happens.

varuns46785756
Level 5
February 26, 2016

Hi Praveen,

1. service is registered in OSGI console

2. bundle is active

3. I am getting error : 

"Only a type can be imported" and 
"The method getContent(String) in the type IContentService is not applicable for the arguments ().."

: please check the way to call the jsonobject , am I doing correct or not:

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

JSONObject finalJsonObjects = servicedata.getContent();

%>

:- thread is not mine but this my friend thread , and this thread is also for the same problem

smacdonald2008
Level 10
February 26, 2016

As i stated - try to place service in new package and try again. Sometimes package conflicts occur. 

However - your code is correct in terms of calling a service. I think its a package issue. I have seen that before. 

Jitendra_S_Toma
Jitendra_S_TomaAccepted solution
Level 10
February 26, 2016

iNTERFACE HAS METHOD WITH STRING.

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

JSONObject finalJsonObjects = servicedata.getContent(); // ARGUMENT IS MISSING HERE. YOUR SHOULD PASS SOMETHING HERE.

%>

smacdonald2008
Level 10
February 26, 2016

Here is a community article that may help what is going on: 

http://www.wemblog.com/2013/05/how-to-perform-system-clean-up-in-adobe.html

I suspect that your project has issues as described in the article. 

To see if this is an issue- try installing your project on a fresh instance of AEM. 

varuns46785756
Level 5
February 28, 2016

Hi,

I have created new new package in new AEM instance , I have tested ,

upto calling service nothing is wrong


com.abc.www.core.service.servlets.IContentService servicedata = sling.getService(com.abc.www.core.service.servlets.IContentService.class);

(I am getting error when I am adding " JSONObject finalJsonObjects = servicedata.getContent(); " 

:I have tested with passing parameter also but getting the error:"

"Only a type can be imported" and 
"The method getContent(String) in the type IContentService is not applicable for the arguments ().."

 

:Return type in my service class is JSONObject and problem is only with my way to call the jsonobject. with this line: "JSONObject finalJsonObjects = servicedata.getContent();"

Please let me know how can I call JSONObject in my component jsp.

 

Regards,

 

 

 

%>