Expand my Community achievements bar.

SOLVED

AEM -sling servlet JSON extension

Avatar

Level 3

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.

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

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

View solution in original post

10 Replies

Avatar

Correct answer by
Employee Advisor

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

Avatar

Level 3

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.

Avatar

Community Advisor

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

Avatar

Employee Advisor

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

Avatar

Community Advisor

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

Avatar

Level 2

I understad the explanation from @Jörg_Hoh 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 ?

Avatar

Level 2

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)

Avatar

Employee Advisor

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