Expand my Community achievements bar.

SOLVED

AEM6.4 @Reference not working in SlingServlet

Avatar

Level 7

Hi  All,

My sling servlet is not working as soon as i add

@Reference in it, otherwise it works fine

Below is the servlet code, i have create custom configuration as per the AEM6.4 standards(one @Interface class, one Interface class, one Impl class with proper annotations(org.osgi.service.component.annotations))

@Component(service = Servlet.class,

property = { Constants.SERVICE_DESCRIPTION + "=Simple Demo Servlet",

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

"sling.servlet.paths=" + "/bin/testservlet" }

)

public class TestServlet extends SlingAllMethodsServlet {

  private static final long serialVersionUID = 1L;

 

  @Reference

  MySimpleService mySimpleService;

 

  private static final Logger logger =

  LoggerFactory.getLogger(TestServlet.class);

 

  @Override

  protected void doGet(final SlingHttpServletRequest req, final

  SlingHttpServletResponse resp) throws ServletException, IOException {

  logger.info("Inside doGet!!!");

  try {

  logger.info("Name ::: "+mySimpleService.getMyName());

  resp.getWriter().write(mySimpleService.getMyName()); }

  catch(Exception ex) {

  logger.error("Error in  doGet ::: ", ex);

  }

  }

}

 

Please advise, any help is highly appreciated.

Thanks,

Pradeep

1 Accepted Solution

Avatar

Correct answer by
Level 10

Look at our SOLR Article. This one closely matches your requirement. We have a class named MySimpleService that reads OSGi config values.

In the Servlet in this article - we use a @Refence to get config values:

@Reference

    SolrServerConfiguration solrConfigurationService;

This shows the proper way of getting OSGI configuration information within an AEM Servlet. Never try and read OSGI config information directly from a servlet. Write a separate class - as shown in this development article.

Adobe Experience Manager Help | Integrating SOLR with Adobe Experience Manager 6.4

Hope this clear this issue up. Notice the Java code in this Servlet. We use the CONFIG values to perform SOLR actions - such as getting the URL to the SOLR server.

            final String protocol = solrConfigurationService.getSolrProtocol();

        final String serverName = solrConfigurationService.getSolrServerName();

        final String serverPort = solrConfigurationService.getSolrServerPort();

        final String coreName = solrConfigurationService.getSolrCoreName();

        final String pagesResourcePath = solrConfigurationService

I would go through this entire article so you understand.

Hope this helps you.

View solution in original post

25 Replies

Avatar

Community Advisor

Hi @SanketJa1 
Please check the imports 

import org.osgi.service.component.annotations.Reference; 



Arun Patidar

Avatar

Level 7

I finally solved this, though it was very weird, first we have to save the configuration then servlet will become in  'satisfied' state.

Once servlet is in satisfied state we can call the same in url.

Thanks everyone for helping me on this.

Regards,

Pradeep

Avatar

Community Advisor

The reason is your not setting default value of configuration and service returning null.

You can use the configuration like I mentioned to set default value of property and servlet will work without saving osgi value.

example code I posted in last comment.



Arun Patidar

Avatar

Level 7

I had set default value(i have  shared all my code in post), still i have to open the configuration and save once to bring servlet in satisfied state.

Avatar

Community Advisor

Hi,

you were using below which was returning null

@AttributeDefinition(name = "Test Namel", defaultValue="Pradeep", description = "Read my name")

    String name();

But I changed your config class like below, I was getting results

@AttributeDefinition(

        name = "Test Name",

        description = "Read my Name",

       type = AttributeType.STRING

    )

String getName() default "Pradeep";



Arun Patidar