【问题标题】:Magnolia REST Endpoint for Categories类别的 Magnolia REST 端点
【发布时间】:2022-10-05 16:04:52
【问题描述】:

我现在明白了,Magnolia 是如何工作的。我用 Java 为 Magnolia Content App 编写 REST Endpoint。 我的内容应用程序是类别 - 应用程序,它看起来像这样:

  • cat-1:cat-1、cat-4;
  • cat-3:\"\"

我的任务是,为 Java 中的 Categories 应用程序定义一个 REST 端点,它根据传递的类别名称提供子类别并将它们显示在组件中。 例如:如果你输入 \"GET \"cat-1\"\" 那么你会得到一个 JSON-Array [\"cat-1\", \"cat-4\"]

请帮我理解木兰中的rest api机制。 我的这个项目的代码:

@Api(CategoryEndpoint.URI)

@Path(CategoryEndpoint.URI) 公共类 CategoryEndpoint 扩展 DbEndpoint {

public static final String URI = \"/categories\";


private final DamTemplatingFunctions damfn;
private final TemplatingFunctions cmsfn;
private final PathNormalizer pathNormalizer;

private static final Logger log = LoggerFactory.getLogger(CategoryEndpoint.class);

/**
 * The default constructor
 *
 * @param endpointDefinition
 * @param damfn
 * @param cmsfn
 * @param pathNormalizer
 * @param responseBuilderFactory
 */
@Inject
public CategoryEndpoint(EndpointDefinition endpointDefinition,
                        DamTemplatingFunctions damfn,
                        TemplatingFunctions cmsfn, PathNormalizer pathNormalizer,
                        DbResponseBuilder.InstanceFactory responseBuilderFactory){
    super(endpointDefinition, responseBuilderFactory);
    this.damfn = damfn;
    this.cmsfn = cmsfn;
    this.pathNormalizer = pathNormalizer;
}

/**
 *
 * @param path
 * @return the matching categories
 * @throws RepositoryException of the session cannot retrieved
 */
@GET
@Path(\"/categories\")
@Produces({MediaType.APPLICATION_JSON})
public Response getCategory(@QueryParam(\"path\") @DefaultValue(\"/cat1\") String path) throws RepositoryException {
    var session = MgnlContext.getJCRSession(\"categories\");

    final List<CategoryItem> result= searchForCategory(session,pathNormalizer.normalizePath(path));

    return responseBuilderFactory.newInstance(Response.ok(result)).cachingHeaders().build();
}


private List<CategoryItem> searchForCategory(
    Session session,
    String parentPath) {
    try{
        Iterable<Node> nodes = NodeUtil.collectAllChildren(
            session.getNode(parentPath),
            new NodeTypePredicate(\"cms:category\")
        );
        return null;
    }catch (RepositoryException e) {
        log.debug(\"Failed to find category at path \" + parentPath, e);

        return Collections.emptyList();
    }
};

公共静态类 CategoryItem {

    public final String category;

  public CategoryItem(String category) {
      this.category = category;
  }

}

    标签: java rest endpoint magnolia


    【解决方案1】:

    这应该已经使用 Magnolia 提供的默认端点工作,使用 url http://your_instance/.rest/nodes/v1/category/cat-1?depth=2 (假设路径 /cat-1/cat-4 存在)。但是,您也会获得许多不必要的属性。
    更好的选择是按照documentation 中的说明配置交付端点。

    可能更好的选择是安装 GraphQL module 并使用 GraphQL 来定义和获得您所需要的。

    从长远来看,这些选项中的任何一个都比编写自己的端点更快、更容易维护。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多