Search code examples
aemsling

Sling servlet invalid recursion selector with json extension


I am registering a Sling Servlet using

@Component(service=Servlet.class,
           property={
                   Constants.SERVICE_DESCRIPTION + "=Simple Demo Servlet",
                   "sling.servlet.methods=" + HttpConstants.METHOD_GET,
                   "sling.servlet.resourceTypes="+ "myapp/components/structure/page",
                   "sling.servlet.extensions=" + "json",
                   "sling.servlet.selectors=" + "myselector"
 })

But when calling URL <host:port>/content/myapp/en.myselector.js I get error

Invalid recursion selector value 'myselector'
Cannot serve request to /content/myapp/en.myselector.json in 
org.apache.sling.servlets.get.DefaultGetServlet

If we remove "sling.servlet.extensions=" + "json", from annotation, we are able to hit the servlet using <host:port>/content/myapp/en.myselector.js or <host:port>/content/myapp/en.myselector.html but not <host:port>/content/myapp/en.myselector.json

Any additional configurations required? or its conflicting with DefaultGetServlet? We were trying to expose JSON out of resource using servlet and JSON extension semantically making sense.


Solution

  • Since you've registered the servlet against the resource instead of a path, the resource is actually the jcr:content node of the page, rather than the page node itself.

    Hence, the servlet should work when accessed with the jcr:content in the URL as shown below

    <host:port>/content/myapp/en/_jcr_content.myselector.json