Hi masters!
I am going to implement a REST API in AEM to integrate 3rd party applications.
I do not want to rely in the JCR repository hierarchy, because my internal hierarchy does not matter to the 3rd party applications. So, I do not want to map my Sling servlets to a resourceType (that would mean to specify the path to the resource, so using the JCR hierarchy).
So, I need to map the servlets to a path.
Following the REST API development best practices, I should use a path like ‘/api/v1/articles’ to implement operations like search and create new article (with GET and POST HTTP methods), and a path like ‘/api/v1/articles/<article_id>’ to implement operations like get details and modify a specific article (also with GET and POST HTTP methods).
My question is, is it possible to map Sling servlets to a paths like those, where the <article_id> is variable?
‘/api/v1/articles’
‘/api/v1/articles/<article_id>’
Thank you masters!
J.
Solved! Go to Solution.
Views
Replies
Total Likes
@Julio_Baixauli, I am sorry to say but the out of the box Sling Servlet does not support path with of an path param via {id}. I would implement your requirement from /api/v1/articles/<article-id> as "/api/v1/articles?id="; this uses the URL query string. The full documentation can be found here, https://sling.apache.org/documentation/the-sling-engine/servlets.html
While JAX-RS connector can be integrated to use @PathParam syntax; taking dynamic parts of the request as suffix is a good option too. With suffix, the servlet request would be /api/v1/articles.json/<article_id> and then read the article id in the servlet with request.getRequestPathInfo().getSuffix()
You can find the details here:
Yes it is possible, Adding to given solutions, we can also try to use GET call query params / POST call form parameters or selectors
i.e. '/api/v1/articles.json?id=id123' or ‘/api/v1/articles.id123.json'
I think by using selector or suffix we can use caching but at same time will need to manage cache clear set up as required when any data changes (or just have ttl set).
See https://sling.apache.org/apidocs/sling5/org/apache/sling/api/SlingHttpServletRequest.html#getRequest... and https://sling.apache.org/apidocs/sling5/org/apache/sling/api/request/RequestPathInfo.html for retrieving the variable values
Hi @Julio_Baixauli,
yes it is doable but only with the help of query parameter for <article_id> or by doing some string manipulation to get the <article_id> to pass as selector to invoke the appropriate servlet.
With Jax-rs connector also you can not because the available library for the same is using very old version of some dependency, those are not compatible with the current version of aem. so you will have to build is fresh before using it. then again some request filter will be required to handle the authentication an all but again here also request suffix will not work.
Hope this will help.
Umesh Thakur
Hi Julio,
Since AEM is a JCR the path of the content node would be the apt identifier to be used. For example it would be as below -
/api/v1/articles/<article content path>
Thanks,
Madhu
Views
Replies
Total Likes
JAX-RS can be used in this case, refer https://www.albinsblog.com/2019/06/how-to-expose-restful-services-through-jax-rs-in-aem.html#.X_M4Bd... for more details.
Another option is to rewrite the user-friendly URL from browser to internal servlet URL(based on query parameter) in Dispatcher/Apache, refer to this URL for more details https://www.albinsblog.com/2017/11/rewrite-rules-with-ptpass-through-flag-notwork-in-aem.html#.X_OQX...
Regards
Albin I
@Julio_Baixauli, I am sorry to say but the out of the box Sling Servlet does not support path with of an path param via {id}. I would implement your requirement from /api/v1/articles/<article-id> as "/api/v1/articles?id="; this uses the URL query string. The full documentation can be found here, https://sling.apache.org/documentation/the-sling-engine/servlets.html