Gotcha: Sling Servlet Requires a Name

Published on by Dan KlcoPicture of Me Dan Klco

I recently had the opportunity to delve into the code for the Sling Resources to document the @SlingServlet annotation. One of the more interesting things I discovered was that in addition to everything stated on the Sling documentation about what is required to register a servlet, there is one additional requirement.

Each Servlet which is to be created and registered, must have an unique name. The SlingServletResolver, which handles the resolution of Servlets, generates the name based on one of the following properties (in order):

  • sling.core.servletName
  • component.name
  • service.pid
  • service.id

The value of this property must be **explicitly **set for the servlet service. It will not use the default value, which is the fully-qualified class name. If this value is not set, your service will register correctly within OSGi, will seem to be configured correctly, but will not be registered as available in the SlingServletResolver.

Setting the property is as easy as setting the name attribute in the @Component or @SlingServlet annotation:

@SlingServlet(label = "Test Servlet", metatype = true, methods = { "POST",
		"GET" }, paths = "/bin/testservlet", name="com.myco.TestServlet")
@Properties({
		@Property(name = "service.vendor", value = "My Co"),
		@Property(name = "service.description", value = "Servlet testing Sling") })
public class TestServlet extends SlingAllMethodsServlet {

So, if you have created a servlet which seems to be configured properly, but is not appearing, make sure you have one of the name properties set so the Sling Servlet Resolver will pick it up


Tags


comments powered by Disqus