AEM -sling servlet JSON extension

sundarig9086821

22-07-2019

Hi,

We have created a servlet with extension as JS or html.

@SlingServlet(resourceTypes = "project/components/sample", selectors = "sample",

    extensions = "js", methods = "GET")

Resource type is template resource type.

Hence if we access the servlet via /content/project/en.sample.js the servlet responds as expected.

The same works for /content/project/en.sample.html

If we change the extension to JSON the servlet is accessible only when we access  /content/project/en/_jcr_content.sample.json

What is the cause of it to look from jcr:content instead of page path( /content/project/en)?

What can be done if we need to access the servlet as /content/project/en.sample.json

Thanks in advance!!

arunpatidar26, Ratna Kumar, smacdonald2008

JaideepBrar

Kindly help on this..

Thanks,
Sundari.

Accepted Solutions (1)

Accepted Solutions (1)

Jörg_Hoh

Employee

23-07-2019

because /content/project/en does not have the resourceType /project/components/sample 😐

If you want to invoke it directly on a page, you can either omit the resourceType setting the annotation or exchange it by "cq/page".

Answers (5)

Answers (5)

Arun_Patidar

MVP

23-07-2019

Thanks jod91570829

I didn't had an idea about registering servlet using node type. Good to know.

I tried this and worked like a charm.

@Component(service = Servlet.class, property = { Constants.SERVICE_DESCRIPTION + "=Simple Demo Page Servlet",

  "sling.servlet.methods=" + HttpConstants.METHOD_GET, "sling.servlet.resourceTypes=" + "cq/Page",

  "sling.servlet.selectors=" + "sample", "sling.servlet.extensions=" + "js" })

public class SimplePageServlet extends SlingSafeMethodsServlet {

  private static final long serialVersionUID = 1L;

  @Override

  protected void doGet(final SlingHttpServletRequest req, final SlingHttpServletResponse resp)

  throws ServletException, IOException {

  resp.setContentType("text/plain");

  resp.getWriter().write("Title = " + req.getResource().getChild("jcr:content").adaptTo(ValueMap.class).get("jcr:title"));

  }

}

Jörg_Hoh

Employee

23-07-2019

@Arun: If you want it to work only on pages, use the resourceType "cq/page".

Hint: Nodetypes can also be used as resourcetypes, if you replace the ":" with a "/".

Arun_Patidar

MVP

23-07-2019

Hi,

Jorg is right, if you want to access only with page without jcr:content the create a genric servlet with selector and extension

and in the code get jcr:content node and write logic.

e.g.

aem63app-repo/java/page/json at master · arunpatidar02/aem63app-repo · GitHub

@Component(service = Servlet.class, property = { Constants.SERVICE_DESCRIPTION + "=JSON renderer Servlet",
"sling.servlet.methods=" + HttpConstants.METHOD_GET, "sling.servlet.resourceTypes=" + "sling/servlet/default",
"sling.servlet.selectors=" + "hcms", "sling.servlet.extensions=" + "json" })

sundarig9086821

23-07-2019

Hi @joerg,

Thanks for the reply.

If that's the case, how does the below snippet works:

@SlingServlet(resourceTypes = "project/components/sample", selectors = "sample",

    extensions = "js", methods = "GET")

@SlingServlet(resourceTypes = "project/components/sample", selectors = "sample",

    extensions = "html", methods = "GET")

Here also if i access /content/project/en the resourceType is not /project/components/sample.. It looks for resource from jcr:content and works fine on hitting /content/project/en.sample.js or /content/project/en.sample.html

Thanks,

Sundari.