Unable to reference the osgi service into a servlet | Community
Skip to main content
Parusharam
Level 2
Oct 4, 2018 12:43 PM UTC
Solved

Unable to reference the osgi service into a servlet

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.
VeenaVikraman
VeenaVikramanAccepted solution
New Participant
Oct 4, 2018 1:53 PM UTC

Are you trying to reference SlingHttpServletRequest in your service ?

12 replies

VeenaVikraman
New Participant
Oct 4, 2018 2:25 PM UTC

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

Translate
Level 2
Oct 4, 2018 2:21 PM UTC

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" ?

Translate
Parusharam
Level 2
Oct 4, 2018 2:04 PM UTC

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.

Translate
VeenaVikraman
VeenaVikramanAccepted solution
New Participant
Oct 4, 2018 1:53 PM UTC

Are you trying to reference SlingHttpServletRequest in your service ?

Translate
VeenaVikraman
New Participant
Oct 4, 2018 1:49 PM UTC

This is your service or servlet ?

Translate
Parusharam
Level 2
Oct 4, 2018 1:39 PM UTC

Translate
VeenaVikraman
New Participant
Oct 4, 2018 1:30 PM UTC

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

Translate
Parusharam
Level 2
Oct 4, 2018 1:29 PM UTC

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..?

Translate
VeenaVikraman
New Participant
Oct 4, 2018 1:24 PM UTC

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

Translate
Parusharam
Level 2
Oct 4, 2018 1:22 PM UTC

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

Translate