Unable to reference the osgi service into a servlet | Community
Skip to main content
Parusharam
New Participant
October 4, 2018
Solved

Unable to reference the osgi service into a servlet

  • October 4, 2018
  • 12 replies
  • 6557 views

I am trying to reference a service into a servlet,but when I use @Refernce annotation i am getting 404 not found exception even though my service is registered properly.

here is my code snippet

Note: I am using OSGI R6 Annotations in AEM 6.2

Servlet:

--------

@Component(immediate = true,

        service = Servlet.class,

        property = {

                "sling.servlet.paths=/express/bin/getDynamicData",

                "sling.servlet.extensions=json",

                "sling.servlet.methods=GET"

        }

)

@Designate(ocd=DynamicDataServlet.Configuration.class)

public class DynamicDataServlet extends SlingSafeMethodsServlet {

     @Reference

     private DynamicDataService dynamicService;

@Override

    protected void doGet(final SlingHttpServletRequest request,

    final SlingHttpServletResponse response) throws ServletException, IOException {

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

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

          dynamicService.getData(resourceResolver, region) ;

}

}

Service interface:

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

public interface DynamicDataService {

  JSONObject getDynamicData(ResourceResolver resourceResolver, String region);

}

ServiceImpl:

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

@Component

(service = DynamicDataService.class, name = "TNT Dynamic Data Service", immediate =true)

public class DynamicDataServiceImpl implements DynamicDataService {

@Override

  public JSONObject getDynamicData(ResourceResolver resourceResolver, String region) {

     --------

     --------

     -------

}

}

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 VeenaVikraman

Are you trying to reference SlingHttpServletRequest in your service ?

12 replies

smacdonald2008
New Participant
October 4, 2018

We just released new SOLR article that shows how to reference an AEM service from a Servlet where R6 annotations are used.

Servlet:

@Component(service=Servlet.class,

        property={

                Constants.SERVICE_DESCRIPTION + "=Solr Index Servlet",

                "sling.servlet.methods=" + HttpConstants.METHOD_GET,

                "sling.servlet.paths="+ "/bin/solr/push/pages"

           })

public class IndexContentToSolr extends SlingAllMethodsServlet {

private static final long serialVersionUID = 1L;

private static final Logger LOG = LoggerFactory

.getLogger(IndexContentToSolr.class);

@Reference

SolrServerConfiguration solrConfigurationService;

@Reference

SolrSearchService solrSearchService;

SolrSearchService Interface

package com.adobe.aem.core;

import java.io.IOException;

import javax.jcr.RepositoryException;

import org.apache.sling.api.resource.Resource;

import org.apache.sling.commons.json.JSONArray;

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

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

import org.apache.solr.client.solrj.SolrServerException;

import org.apache.solr.client.solrj.impl.HttpSolrClient;

import com.day.cq.search.result.SearchResult;

public interface SolrSearchService {

JSONArray crawlContent(String resourcePath, String resourceType);

JSONArray createPageMetadataArray(SearchResult results)

throws RepositoryException;

JSONObject createPageMetadataObject(Resource pageContent);

boolean indexPageToSolr(JSONObject indexPageData, HttpSolrClient server)

throws JSONException, SolrServerException, IOException;

boolean indexPagesToSolr(JSONArray indexPageData, HttpSolrClient server)

throws JSONException, SolrServerException, IOException;

}

SolrSearchServiceImpl  class

@Component

public class SolrSearchServiceImpl implements SolrSearchService {

private static final Logger LOG = LoggerFactory

.getLogger(SolrSearchServiceImpl.class);

@Reference

private QueryBuilder queryBuilder;

That works.

However - for AEM 6.2 - I have only ever used Felix SRC Annotations - not R6. Not sure if that is the issue.

For AEM 6.2 - we had Maven 10 - which used Felix SRC Annotations.

Peter_Puzanovs
New Participant
October 4, 2018

Dear Parush,

Your example looks fine to me. Just a small check, are you using: import org.osgi.service.component.annotations.Reference; for your reference?

Also, have a look at recent tutorial by Scott where he uses Reference and creates Services and Servlets[0] wit R6 OSGi annotations.

Anyhow, I thought that OSGi R6 were recommended from AEM 6.3 onwards, not AEM 6.2.

[0] Adobe Experience Manager Help | Integrating SOLR with Adobe Experience Manager 6.4

Regards,

Peter