4 Replies Latest reply on Jul 12, 2018 11:12 AM by ravip15579397

    AEM - Java servlet can't register on fresh 6.4 install

    fredm71868943 Level 1

      Hey Everyone,

       

      I've been dealing with a really tricky issue on a fresh AEM 6.4 install. One of my servlets cannot start. It can register fine and shows up okay however, it always throws a 404 at the path its registered at. I deleted and re-installed my AEM and the issue still persists. The error is very cryptic and there isnt much on the internet about it.

       

      Has anyone every seen? "Service factory returned null"

       

      10.05.2018 14:45:04.358 *ERROR* [FelixDispatchQueue] org.apache.sling.servlets.resolver FrameworkEvent ERROR (org.osgi.framework.ServiceException: Service factory returned null. (Component: com.amsurg.core.servlets.RSSXMLServlet (3142)))

      org.osgi.framework.ServiceException: Service factory returned null. (Component: com.amsurg.core.servlets.RSSXMLServlet (3142))

       

      It's nothing with my code and I have several other servlets that register just fine. This particular one was working earlier then just stopped. Anyone have any troubleshooting ideas? I've tried re-building, restarting AEM, and checking the servlet sling checker but nothing seems to be able to find it.

       

      It registers okay and shows up in the bundle as

       

      Service ID 6903Types: javax.servlet.Servlet
      Service PID: com.amsurg.core.servlets.RSSXMLServlet
      Component Name: com.amsurg.core.servlets.RSSXMLServlet
      Component ID: 3142
      Vendor: Adobe

       

      But underneath it there is no part that looks like this

      Types: org.apache.sling.spi.resource.provider.ResourceProvider

      Description: ServletResourceProvider for Servlets at [/libs/amsurg/structure/page/GET.servlet, /libs/amsurg/structure/page/HEAD.servlet]

       

       

      Anyone have any troubleshooting ideas? This is how it is registered - @SlingServlet(paths = "/bin/feed", methods = "GET", metatype = true)

       

      Thanks

      Brendan

        • 1. Re: AEM - Java servlet can't register on fresh 6.4 install
          arunp99088702 Level 4

          I tried to create servlet in AEM6.4, for me its working fine.

          below is sample servlet code which I've created

           

           

          @Component(service=Servlet.class,immediate=true,

            property= {

            Constants.SERVICE_DESCRIPTION + "=TitleSlindServlet Demo Servlet",

            "sling.servlet.paths=/bin/aem64app/titleservlet",

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

                      "sling.servlet.extensions=" + "html"

            })

          public class TitleSlingServlet extends SlingSafeMethodsServlet{

            private static final long serialVersionUID = 1L;

           

            @Override

              protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {

            response.setHeader("Content-Type", "text/html");

              response.getWriter().print("<h1>Sling Servlet injected this title");

            response.getWriter().close();

              }  

           

          }

           

           

          Thanks

          Arun

          • 2. Re: AEM - Java servlet can't register on fresh 6.4 install
            kautuksahni Adobe Employee

            Can you please share your code? and full error.log?

            Team would require Source code to check this up. Maybe you using service which is no longer available in AEM.

            -Kautuk

            • 3. Re: AEM - Java servlet can't register on fresh 6.4 install
              smacdonald2008 Adobe Employee

              For more current versions of AEM - you should be DS annotations ( org.osgi.service.component.annotations.Component) to create Servlets.

               

              /**

              * Servlet that writes some sample content into the response. It is mounted for

              * all resources of a specific Sling resource type. The

              * {@link SlingSafeMethodsServlet} shall be used for HTTP methods that are

              * idempotent. For write operations use the {@link SlingAllMethodsServlet}.

              */

              @Component(service=Servlet.class,

                      property={

                              Constants.SERVICE_DESCRIPTION + "=Simple Demo Servlet",

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

                              "sling.servlet.paths="+ "/bin/myDataSourcePoolServlet"

                         })

              public class SimpleServlet extends SlingAllMethodsServlet {

               

              See this article for an example -- Scott's Digital Community: Creating a Mail List Component for the Experience Manager Toy Store Site

              • 4. Re: AEM - Java servlet can't register on fresh 6.4 install
                ravip15579397 Level 2

                Also make sure you are not using JcrResourceResolverFactory or loginadministrator to get resourceResolver object and use system user to get the resolver as shown below

                 

                Map<String, Object> param = new HashMap<String, Object>();

                param.put(ResourceResolverFactory.SUBSERVICE, "user");

                ResourceResolver resolver = null;

                try {

                     resolver = resolverFactory.getServiceResourceResolver(param);

                     session = resolver.adaptTo(Session.class);

                }...

                 

                Hope it helps!