I understanding now, how the Magnolia works. I writing REST Endpoint for Magnolia Content App in Java. My Content App is Categories - App and it looks like this:
- cat-1: cat-1, cat-4;
- cat-3: ""
My task is, to define a REST endpoint for the Categories app in Java, which delivers the subcategories based on a passed category name and displays them in a component. For example: if you enter "GET "cat-1"" then you get a JSON-Array ["cat-1", "cat-4"]
Please help me understand the rest api mechanism in magnolia. My code for this Project:
@Api(CategoryEndpoint.URI)
@Path(CategoryEndpoint.URI) public class CategoryEndpoint extends 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();
}
};
public static class CategoryItem {
public final String category;
public CategoryItem(String category) {
this.category = category;
}
}
CodePudding user response:
This should work already using the default endpoint provided by Magnolia, using url http:///.rest/nodes/v1/category/cat-1?depth=2 (assuming the path /cat-1/cat-4
exists). However you get a lot of unnecessary properties with it as well.
Better option is to configure delivery end point at mentioned in documentation.
Possibly yet better option is to have GraphQL module installed and use GraphQL to define and get exactly what you need.
Either of those options is faster and easier to maintain in the long run than writing your own endpoint.