如何在 adobe CQ 中创建自定义 json 响应?

How to create a custome json response in adobe CQ?

我希望能够 return json 响应一些特定的动态数据。我很难理解这样做所涉及的步骤。这不是为了获取 CQ 自动执行的节点详细信息。

目标是 json 响应 return 像这样的请求 http:///def/getMyInfo.json

如果您想在组件级别执行此操作,方法是在组件中创建一个 JSON.jsp。

在 jsp 中你会得到类似

的内容
<%@ page import="org.apache.sling.commons.json.io.*" %>
<%@include file="/libs/foundation/global.jsp" %>
<% 
    response.setContentType("application/json");
    response.setCharacterEncoding("utf-8");

    JSONWriter writer = new JSONWriter(response.getWriter());
    writer.object();
    writer.key("Name");
    writer.value("English");
    writer.key("Code");
    writer.value("en");
    writer.endObject();
%>

那么如果你想访问 json,你可以访问它 http://localhost:4502/content/geometrixx/en/jcr:content/mycomponentinstance.json

如果你想让它更通用,你可以将 JSON.jsp 添加到页面组件而不是普通组件,然后你需要调用 http://localhost:4502/content/geometrixx/en/jcr:content.json 而不是。

我想更像这样访问它 - 没有 jcr:content

http://localhost:4502/content/geometrixx/en/mycomponentinstance.json

我创建了一个页面 mycomponentinstance,它有一个 sling 资源组件 mycomponentinstance。该组件有一个文件 mycomponentinstance.json.jsp,其中包含我的 json 响应。它也有 Page.json,我想这是让它识别这个东西的关键。