AEM -sling servlet JSON extension | Community
Skip to main content
Level 2
July 23, 2019
Solved

AEM -sling servlet JSON extension

  • July 23, 2019
  • 7 replies
  • 7878 views

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.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by joerghoh

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".

7 replies

joerghoh
Adobe Employee
joerghohAdobe EmployeeAccepted solution
Adobe Employee
July 23, 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".

Level 2
July 23, 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.

arunpatidar
Community Advisor
Community Advisor
July 23, 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" })
Arun Patidar
joerghoh
Adobe Employee
Adobe Employee
July 23, 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 "/".

arunpatidar
Community Advisor
Community Advisor
July 23, 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"));

  }

}

Arun Patidar
Level 2
July 24, 2019

Thanks arunpatidar26​ & joerg,

Thanks for the info.

Level 2
November 18, 2021

I understad the explanation from @joerghoh but I have some doubs, the same servlet with the same resourcetype, and selectors but with extension JSON I need to use /jcr:content.selector.json but! with for example extension XML is not necessary with XML I can do content/myproject/en/article1.selector.xml without use jcr:content. How it's works ? Have sense this special behaviour only for extension JSON ?

joerghoh
Adobe Employee
Adobe Employee
November 18, 2021
joerghoh
Adobe Employee
Adobe Employee
November 18, 2021

Yes! For me the real solution to the topic is your last answer. An additional doubt. Is a good practice apply an overlay using Page.json.jps (with proxy inside)


it should just be a simple 2-liner (invoking the proxy.jsp)... I think that's acceptable.