Skip to main content
Level 5
Apr 1, 2019 5:41 PM UTC
Solved

AEM6.4 @Reference not working in SlingServlet

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
smacdonald2008
smacdonald2008Accepted solution
Level 10
Apr 2, 2019 6:00 PM UTC

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.

25 replies

arunpatidar
Community Advisor
Community Advisor
Apr 3, 2019 7:45 AM UTC

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.

Translate
Arun Patidar
Level 5
Apr 3, 2019 3:59 PM UTC

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.

Translate
arunpatidar
Community Advisor
Community Advisor
Apr 3, 2019 4:11 PM UTC

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

Translate
Arun Patidar