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

VeenaVikraman
New Participant
October 4, 2018

If you add that policy , your service will be resolved only if you have all the required services available.

New Participant
October 4, 2018

Hi, Can you just give a quick check to all the dependencies in your parent pom and their versions. Can i know which version are you using for "maven-bundle-plugin" ?

Parusharam
New Participant
October 4, 2018

Yes, i removed it and it's working fine. But If I add Configuration-policy = require then My service is missing in /system/console/components.

VeenaVikraman
VeenaVikramanAccepted solution
New Participant
October 4, 2018

Are you trying to reference SlingHttpServletRequest in your service ?

VeenaVikraman
New Participant
October 4, 2018

This is your service or servlet ?

Parusharam
New Participant
October 4, 2018

VeenaVikraman
New Participant
October 4, 2018

Can you share the screen shot of the service from the system console. There should be some reference which will help us what is causing the service not to resolve

Parusharam
New Participant
October 4, 2018

Yes looks like issue with the service as it is showing unsatisfied (reference). When I tried to make it active it is going to disabled state.

Do I need create ocd class for service as well..?

VeenaVikraman
New Participant
October 4, 2018

Agree with PuzanovsP​ and smacdonald2008​ . + Could you please check your system/console/components and make sure your service is active .

Parusharam
New Participant
October 4, 2018

Hi, Yes, I am using import org.osgi.service.component.annotations.Reference annotation only.