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

edubey
Level 10
February 28, 2016

1. Make sure you pass parameter as the method in interface accepts the parameter.

2. Have jar file in your maven pom which helps you to use JSONObject 

3. import json that class in jsp

Jitendra_S_Toma
Level 10
February 29, 2016

Provide code of interface & your Implementation. Also mention component jsp where service method is being invoked.

---Jitendra

varuns46785756
Level 5
February 29, 2016
IContentService.java : interface ---------------------------------- 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.abc.www.core.service.exceptions.ContentServiceExceptions; public interface IContentService { public JSONObject getContent(String path) throws ContentServiceExceptions,RepositoryException,IOException,JSONException; }
component jsp ----------------- <%@include file="/libs/foundation/global.jsp"%> <%@ page import="org.apache.sling.commons.json.io.*,org.w3c.dom.*" %> <%@taglib prefix="cq" uri="http://www.day.com/taglibs/cq/1.0" %> <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@page import="com.abc.www.core.service.servlets.IContentService,org.apache.sling.commons.json.JSONException,org.apache.sling.commons.json.JSONObject"%> <%@ page import="org.apache.sling.commons.json.io.*,com.adobe.cq.*" %> <% com.abc.www.core.service.servlets.IContentService servicedata = sling.getService(com.abc.www.core.service.servlets.IContentService.class); JSONObject jsobject= servicedata.getContent(path); %>
varuns46785756
Level 5
February 29, 2016

please find attached ContentService.java class

Jitendra_S_Toma
Level 10
February 29, 2016

Hey Varun,

You don't have annotations at the getContent() method. That's the reason, service is not able to identify this method. Just just @Override above this method and try.

@Override public JSONObject getContent(String path) throws ContentServiceExceptions, RepositoryException, IOException, JSONException { methodName = "getContent()"; // if block will be executed if path object is null or empty if(ContentServiceUtil.isEmpty(path)){ throw new ContentServiceExceptions("["+className+"."+methodName+"] "+ContentServiceConstants.EMPTY_PATH_TYPE_ERROR_MSG); } jsonContent = getStaticContent(path); return jsonContent; }

-----------------------------------------------------------------------------------------------

--------Jitendra

varuns46785756
Level 5
March 4, 2016

thanks Jitendra, 

its working , thank you :)