AEM-如何以JSON格式返回页面信息

时间:2018-10-13 04:25:58

标签: aem cq5

我需要以JSON格式显示给定路径中所有子页面和子页面的标题和名称。请提供实施。

2 个答案:

答案 0 :(得分:0)

首先,您必须自己尝试一些事情,然后才寻求帮助,而不是完整的解决方案!无论如何,那里有一些解决方案。

根据Adobe,您可以在此处以JSON格式实现页面信息:

package com.adobe.example;

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.Service;
import org.apache.felix.scr.annotations.Reference;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;

import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;

import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageInfoProvider;

@Component(metatype = false)
@Properties({
    @Property(name="service.description", value="Returns the public URL of a resource.")
})
@Service
public class PageUrlInfoProvider implements PageInfoProvider {


    @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY)
    private com.day.cq.commons.Externalizer externalizer;

    private String fetchExternalUrl(ResourceResolver rr, String path) {
        return externalizer.publishLink(rr, path);
    }

    public void updatePageInfo(SlingHttpServletRequest request, JSONObject info, Resource resource)
            throws JSONException {

        Page page = resource.adaptTo(Page.class);
        JSONObject urlinfo = new JSONObject();
        urlinfo.put("publishURL", fetchExternalUrl(null,page.getPath()));
        info.put("URLs",urlinfo);
    }
}

或者您可以尝试使用此Page作为解决方案

答案 1 :(得分:0)

请参考以下可能有用的链接:
http://www.nateyolles.com/blog/2015/12/converting-aem-sling-resources-to-json

除了上述解决方案之外,您还使用递归级别以JSON格式获取数据,例如/content/we-retail/language-masters/en。{placeholder}.json

将占位符替换为要打印的节点级别,并在需要时返回JSON。

要了解有关不同格式的渲染数据的更多信息, 请参阅:https://sling.apache.org/documentation/bundles/rendering-content-default-get-servlets.html