title: Custom JSON renderer in AEM/Sling
body: <p>I've been playing around with this for a while now, and I think, I've - almost - cracked it, but I am still not fully satisfied with my solution.</p>
  
  <p>So, what I want to do, is having a piece of content, a list of items, which would have two views: The standard HTML one, so people can view and edit it; and then a JSON endpoint for other services to consume.</p>
  
  <p>First I thought it's a simple matter of creating two JSP scripts to render the content:</p>
  
  <ul>
  <li>/apps/my-stuff/components/list-page/html.jsp</li>
  <li>/apps/my-stuff/components/list-page/json.jsp</li>
  </ul>
  
  <p>However the Apache Sling <code>DefaultServlet</code> seems to be rather ignorant of the <code>json.jsp</code> script.</p>
  
  <p>As a second attempt, I created another script, in <code>/apps/foundation/components/primary/cq/Page/json.jsp</code> which will be actually called, and renders the page, as I expected. However there are a couple of worries/questions regarding this:</p>
  
  <ul>
  <li>First of all, why is this being honoured by the system, and not the one in the more specific place?</li>
  <li>The documentation states, that to find the appropriate renderer, first <code>sling:resourceType</code> will be inspected, then <code>sling:resourceSuperType</code> and then, only as a fallback will <code>jcr:PrimaryType</code> checked. However I think this is rather: <code>jcr:PrimaryType</code>, then the <code>DefaultServlet</code>, and then all the other things.</li>
  <li>Most worryingly however, I have to admit, this is rather generic, so it'll break all the contnet with <code>jcr:PrmaryType = Page</code>, so that could have some side-effects.</li>
  </ul>
  
  <p>A solution could be creating a new type: <code>ListPage extends Page</code>; and then create a renderer for that in <code>/apps/foundation...</code>. However I have this bad feeling, that might introduce other problems.</p>
  
  <p>So my question is two fold: What is the proper way of doing this, and/or what am I missing from the way the <strong>URL -> script</strong> resolution is working in AEM/Sling. (Because it seems to be slightly different that described <a href="https://sling.apache.org/documentation/the-sling-engine/url-to-script-resolution.html" rel="nofollow noreferrer">here</a> and <a href="http://1.bp.blogspot.com/-aTPp4Vt8CYo/U9ZLx4C4X5I/AAAAAAAAAao/BmrEJFdyF80/s1600/image.img.png" rel="nofollow noreferrer">here</a>.)</p>
  
  <p>(Obviously I am trying to keep the default JSON renderer for other pages, as that might be needed for other things in the page. I am not even sure, changing this one page won't break the UI for this particular page...)</p>