AEM6.4 @Reference not working in SlingServlet

Avatar

Avatar

pradeepdubey82

Avatar

pradeepdubey82

pradeepdubey82

01-04-2019

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

View Entire Topic

Avatar

Avatar

pradeepdubey82

Avatar

pradeepdubey82

pradeepdubey82

03-04-2019

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