You are probably looking for the @SlingServletPaths, OSGi DS 1.4 (R7) component property type annotations for Sling Servlets, code/unit test examples and was not successful.
Apache recommends not use the @SlingServletPaths annotation, Sling Servlet register by a path. Rather to use the @SlingServletResourceTypes component type. Given the drawbacks in the caveats below, it is strongly recommended to bind servlets to resource types rather than paths.
Caveats when using @SlingServletPaths:
- Path-bound servlets cannot be access-controlled using the default JCR repository ACLs.
- Path-bound servlets can only be registered to a path and not a resource type (i.e. no suffix handling).
- If a path-bound servlet is not active, e.g. if the bundle is missing or not started, a POST might result in unexpected results. usually creating a node at /bin/xyz which subsequently overlays the servlets path binding.
- The mapping is not transparent to a developer looking just at the repository.
Also, have a look at the Apache Sling Servlet Documentation for the SlingServletPaths component property type, which what is mentioned above is clearly stated.
Example of @SlingServletResourceTypes:
The proper way to bind Sling Servlets is by binding the servlet to resource types. This is an example of a servlet binding to a resource type.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | package com.sourcedcode.core.servlets.impl; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.SlingHttpServletResponse; import org.apache.sling.api.servlets.SlingSafeMethodsServlet; import org.apache.sling.servlets.annotations.SlingServletResourceTypes; import org.osgi.service.component.annotations.Component; import javax.servlet.Servlet; import java.io.IOException; import static org.apache.sling.api.servlets.HttpConstants.METHOD_GET; /** * Enables all resources to return formatted response data from the doGet() method. * Appending ".example.json" on any resource will activate the doGet() method below. */ @Component(service = Servlet.class) @SlingServletResourceTypes( resourceTypes = "sling/servlet/default", methods = "get", extensions = "json", selectors = "example") public class SlingServletResourceTypesExampleServlet extends SlingSafeMethodsServlet { @Override protected void doGet(SlingHttpServletRequest req, SlingHttpServletResponse res) throws IOException { res.setContentType("text/plain"); res.setCharacterEncoding("UTF-8"); res.setStatus(200); res.getWriter().write("Done"); } } |
For an example of how to write a unit test for a sling servlet,binding by resource type, click here.
I will never register by path!
Yeah, say no to Paths!
Yeah resource types all the way!
thanks bro