【发布时间】: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