Registering @SlingServletPaths Component Property Type

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.

Apache’s Documentation

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");
    }
}
Unit Test for @SlingServletResourceTypes
For an example of how to write a unit test for a sling servlet,binding by resource type, click here.

Hello, I am an enthusiastic Adobe Community Advisor and a seasoned Lead AEM Developer. I am currently serving as an AEM Technical Lead at MNPDigital.ca, bringing over a decade of extensive web engineering experience and more than eight years of practical AEM experience to the table. My goal is to give back to the AEM Full Stack Development community by sharing my wealth of knowledge with others. You can connect with me on LinkedIn.

4 thoughts on “Registering @SlingServletPaths Component Property Type

Leave a Reply

Your email address will not be published. Required fields are marked *


Back To Top