Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn More

View all

Sign in to view all badges

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

Avatar

Avatar
Give Back 5
Level 3
Julio_Baixauli
Level 3

Likes

22 likes

Total Posts

82 posts

Correct Reply

6 solutions
Top badges earned
Give Back 5
Give Back 3
Give Back
Ignite 1
Validate 25
View profile

Avatar
Give Back 5
Level 3
Julio_Baixauli
Level 3

Likes

22 likes

Total Posts

82 posts

Correct Reply

6 solutions
Top badges earned
Give Back 5
Give Back 3
Give Back
Ignite 1
Validate 25
View profile
Julio_Baixauli
Level 3

31-12-2020

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.

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Establish
MVP
BrianKasingli
MVP

Likes

615 likes

Total Posts

592 posts

Correct Reply

232 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile

Avatar
Establish
MVP
BrianKasingli
MVP

Likes

615 likes

Total Posts

592 posts

Correct Reply

232 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile
BrianKasingli
MVP

04-01-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

 

Answers (6)

Answers (6)

Avatar

Avatar
Validate 1
MVP
Albin_Issac
MVP

Likes

106 likes

Total Posts

119 posts

Correct Reply

33 solutions
Top badges earned
Validate 1
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile

Avatar
Validate 1
MVP
Albin_Issac
MVP

Likes

106 likes

Total Posts

119 posts

Correct Reply

33 solutions
Top badges earned
Validate 1
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile
Albin_Issac
MVP

04-01-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_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

www.albinsblog.com

Avatar

Avatar
Springboard
Level 6
snbaem
Level 6

Likes

92 likes

Total Posts

217 posts

Correct Reply

29 solutions
Top badges earned
Springboard
Establish
Validate 25
Validate 10
Validate 1
View profile

Avatar
Springboard
Level 6
snbaem
Level 6

Likes

92 likes

Total Posts

217 posts

Correct Reply

29 solutions
Top badges earned
Springboard
Establish
Validate 25
Validate 10
Validate 1
View profile
snbaem
Level 6

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

Avatar

Avatar
Give Back 10
MVP
shelly-goel
MVP

Likes

183 likes

Total Posts

343 posts

Correct Reply

88 solutions
Top badges earned
Give Back 10
Validate 1
Shape 1
Ignite 1
Give Back 5
View profile

Avatar
Give Back 10
MVP
shelly-goel
MVP

Likes

183 likes

Total Posts

343 posts

Correct Reply

88 solutions
Top badges earned
Give Back 10
Validate 1
Shape 1
Ignite 1
Give Back 5
View profile
shelly-goel
MVP

31-12-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-...

Avatar

Avatar
Boost 1
Level 1
madhusudanr3987
Level 1

Like

1 like

Total Posts

1 post

Correct Reply

0 solutions
Top badges earned
Boost 1
View profile

Avatar
Boost 1
Level 1
madhusudanr3987
Level 1

Like

1 like

Total Posts

1 post

Correct Reply

0 solutions
Top badges earned
Boost 1
View profile
madhusudanr3987
Level 1

04-01-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

Avatar

Avatar
Validate 1
MVP
Umesh_Thakur
MVP

Likes

147 likes

Total Posts

157 posts

Correct Reply

53 solutions
Top badges earned
Validate 1
Applaud 25
Ignite 3
Ignite 1
Give Back 5
View profile

Avatar
Validate 1
MVP
Umesh_Thakur
MVP

Likes

147 likes

Total Posts

157 posts

Correct Reply

53 solutions
Top badges earned
Validate 1
Applaud 25
Ignite 3
Ignite 1
Give Back 5
View profile
Umesh_Thakur
MVP

03-01-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

Avatar

Avatar
Establish
MVP
Anudeep_Garnepudi
MVP

Likes

274 likes

Total Posts

320 posts

Correct Reply

87 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile

Avatar
Establish
MVP
Anudeep_Garnepudi
MVP

Likes

274 likes

Total Posts

320 posts

Correct Reply

87 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile
Anudeep_Garnepudi
MVP

31-12-2020