我在 adobe API 文档中看到了 QueryBuilder 服务,它可以通过 URL 调用许多查询选项,这会返回基于 JSON 的响应。很好。我想在 java API 中使用它。我找到了一些示例并在 Eclipse 中进行了尝试,但有些事情我不知道如何获取服务“吊带”,如下面给定的代码所示:或者换句话说,如何创建 QueryBuilder Java 对象。
我需要的任何特定 JAR 或者我需要在我的 PC 上安装 Apache SLING,我不确定任何可以分享此内容的人是否会有所帮助,因为我是 CQ5 的新手并且不知道。
示例代码如下:
Repository repository = JcrUtils.getRepository(SERVER);
SimpleCredentials credentials = new SimpleCredentials(USERNAME, PASSWORD.toCharArray());
Session session = repository.login(credentials);
System.out.println("got session: " + session);
/*HERE SLING IS THE PROBLEM HOW TO GET SLING */
QueryBuilder qbuilder = sling.getService(QueryBuilder.class);
String fulltextSearchTerm = "Geometrixx";
// create query description as hash map (simplest way, same as form
// post)
Map<String, String> map = new HashMap<String, String>();
// create query description as hash map (simplest way, same as form
// post)
map.put("path", "/content");
map.put("type", "cq:Page");
map.put("group.p.or", "true"); // combine this group with OR
map.put("group.1_fulltext", fulltextSearchTerm);
map.put("group.1_fulltext.relPath", "jcr:content");
map.put("group.2_fulltext", fulltextSearchTerm);
map.put("group.2_fulltext.relPath", "jcr:content/@cq:tags");
// can be done in map or with Query methods
map.put("p.offset", "0"); // same as query.setStart(0) below
map.put("p.limit", "20"); // same as query.setHitsPerPage(20) below
Query query = qbuilder.createQuery(PredicateGroup.create(map), session);
query.setStart(0);
query.setHitsPerPage(20);
SearchResult result = query.getResult();
最佳答案
如果您使用的是 Java 类而不是 JSP,则可以使用 @Reference
注释来查找给定的服务 - 这将找到由 OSGi 注册的匹配服务并将其实现返回给您。
The @Reference annotation defines references to other services made available to the component by the Service Component Runtime.
您的代码将变得简单:
@Reference
QueryBuilder qbuilder;
关于java - CQ5 Java QueryBuilder API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21040299/