The title of the question sounds like you want wanted to get the equivalent of a cURL/AJAX call to /path/to/mycomponent.-1.json
but within a Servlet or other Java class.
You can use the org.apache.sling.commons.json.jcr.JsonItemWriter class to dump a JCR node into a JSONObject with infinite recursion. By passing in a Set into the constructor you can specify which properties to exclude from the final JSON. See http://www.nateyolles.com/blog/2015/12/converting-aem-sling-resources-to-json for more examples.
Resource resource = resolver.getResource("/content/my-app/my-page");
if (resource != null) {
Node node = resource.adaptTo(Node.class);
StringWriter stringWriter = new StringWriter();
JsonItemWriter jsonWriter = new JsonItemWriter(null);
try {
jsonWriter.dump(node, stringWriter, -1);
JSONObject jsonObject = new JSONObject(stringWriter.toString());
} catch (RepositoryException | JSONException e) {
LOGGER.error("Could not create JSON", e);
}
}
You can also use the org.apache.sling.engine.SlingRequestProcessor class as Bertrand discussed above. See http://www.nateyolles.com/blog/2015/10/get-rendered-html-for-an-aem-resource-or-component for more examples.
@Reference
private RequestResponseFactory requestResponseFactory;
@Reference
private SlingRequestProcessor requestProcessor;
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
throws ServletException, IOException {
/* Setup request */
HttpServletRequest req = requestResponseFactory.createRequest("GET",
"/content/my-app/my-page.-1.json");
/* Setup response */
ByteArrayOutputStream out = new ByteArrayOutputStream();
HttpServletResponse resp = requestResponseFactory.createResponse(out);
/* Process request through Sling */
requestProcessor.processRequest(req, resp, request.getResourceResolver());
String jsonString = out.toString();
}