REST API - Map a Sling servlet to a path with a variable part | Community
Skip to main content
Level 4
December 31, 2020
Solved

REST API - Map a Sling servlet to a path with a variable part

  • December 31, 2020
  • 7 replies
  • 4930 views

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.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by BrianKasingli

@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

 

7 replies

shelly-goel
Adobe Employee
Adobe Employee
December 31, 2020

@julio_baixauli 

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:

https://stackoverflow.com/questions/20984706/is-it-possible-to-place-variables-into-a-resource-path-within-a-sling-servlet

Shubham_borole
Community Advisor
Community Advisor
January 3, 2021

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#getRequestParameterMap() and https://sling.apache.org/apidocs/sling5/org/apache/sling/api/request/RequestPathInfo.html for retrieving  the variable values

Umesh_Thakur
Community Advisor
Community Advisor
January 4, 2021

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

January 4, 2021

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

kautuk_sahni
Community Manager
Community Manager
January 11, 2021
Nice answer.
Kautuk Sahni
Community Advisor
January 4, 2021

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_M4BdhKg2w 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_OQX9hKg2w

Regards

Albin I

www.albinsblog.com

BrianKasingli
Community Advisor and Adobe Champion
BrianKasingliCommunity Advisor and Adobe ChampionAccepted solution
Community Advisor and Adobe Champion
January 4, 2021

@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