Hi @shagunmalik!
I assume that you don't want to get a JSON response of only one path but different areas of your content tree.
While there are different ways to register a servlet (see [1]), the recommended way - that should perfectly match your use case - is to bind it to a resource type and not to a path as mentioned under [2]:
"Given these drawbacks it is strongly recommended to bind servlets to resource types rather than paths."
So my recommendation is to create a servlet that binds to your required resource type(s) and to the JSON extension. If you need to be more specific, you can also add a custom selector for differentiation. Your servlet can then access the requested resource, read the required properties from it and return them in the form that you want.
Please also note that there already is an OOTB JSON renderer.
So if you need to access certain properties of your example path /content/abc/xyz/qwe you can just request the following:
- /content/abc/xyz/qwe.1.json
This will return all properties of the "qwe" node in JSON format. If the consumer for that information is able to filter on the required properties and ignore the rest, there is no need for custom implementation on the AEM side. If you need a specific structure of your JSON response the above mentioned approach with a custom servlet is the way to go.
Please find an example implementation based on the AEM Maven Archetype [3] below:
package com.mysite.core.servlets;
import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.apache.sling.servlets.annotations.SlingServletResourceTypes;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.propertytypes.ServiceDescription;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.day.cq.commons.jcr.JcrConstants;
@Component(service = { Servlet.class })
@SlingServletResourceTypes(resourceTypes = "mysite/components/page", methods = HttpConstants.METHOD_GET, extensions = "json")
@ServiceDescription("Simple JSON Servlet")
public class JsonServlet extends SlingSafeMethodsServlet {
private final static Logger LOG = LoggerFactory.getLogger(JsonServlet.class);
@Activate
protected void activate() {
LOG.debug("{} activated.", getClass());
}
@Override
protected void doGet(final SlingHttpServletRequest request, final SlingHttpServletResponse response)
throws ServletException, IOException {
// Getting the current resource
final Resource resource = request.getResource();
// Getting a property
String titleProperty = (String) resource.getValueMap().get(JcrConstants.JCR_TITLE);
// TODO compose your JSON response and write it to the response
response.setContentType("text/plain");
response.getWriter().write("Title = " + titleProperty);
}
}
Hope that helps!
[1] https://sling.apache.org/documentation/the-sling-engine/servlets.html#servlet-registration-1
[2] https://sling.apache.org/documentation/the-sling-engine/servlets.html#caveats-when-binding-servlets-by-path-1
[3] https://github.com/adobe/aem-project-archetype/blob/develop/src/main/archetype/core/src/main/java/core/servlets/SimpleServlet.java