where ware the "new" annotations for servlets?

Avatar

Avatar
Affirm 5
Level 4
TB3dock
Level 4

Likes

37 likes

Total Posts

223 posts

Correct reply

5 solutions
Top badges earned
Affirm 5
Ignite 10
Boost 25
Give Back 25
Validate 10
View profile

Avatar
Affirm 5
Level 4
TB3dock
Level 4

Likes

37 likes

Total Posts

223 posts

Correct reply

5 solutions
Top badges earned
Affirm 5
Ignite 10
Boost 25
Give Back 25
Validate 10
View profile
TB3dock
Level 4

18-04-2021

This page: https://sling.apache.org/documentation/the-sling-engine/servlets.html has an embeded presentation which has servlet declarations like this:

 

@SlingServiceResourceTypes(resourceTypes="xx/yy/zz") etc.

 

However, all the examples and AEM docs seem to only support this older format:

 

@component(service = Servlet.class, property = { Constants.SERVICE_DESCRIPTION + "=something",

"sling.servlet.methods=" + HttpConstants.METHOD_GET, "sling.servlet.paths=" + "/bin/api/mypath" })

 

Is this new format available in the latest cloud SDK, and are there any examples of it does anyone know?

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Applaud 25
Level 10
asutosh_jena
Level 10

Likes

593 likes

Total Posts

705 posts

Correct reply

207 solutions
Top badges earned
Applaud 25
Give Back 100
Boost 500
Affirm 100
Ignite 1
View profile

Avatar
Applaud 25
Level 10
asutosh_jena
Level 10

Likes

593 likes

Total Posts

705 posts

Correct reply

207 solutions
Top badges earned
Applaud 25
Give Back 100
Boost 500
Affirm 100
Ignite 1
View profile
asutosh_jena
Level 10

18-04-2021

Hi @TB3dock 

 

OSGi DS 1.4 (R7) component property type annotations for Sling Servlets (recommended) is supported from bnd-maven-plugin 4.1.0+ and maven-bundle-plugin 4.1.0+ and this is the recommended approach to write the Servlet. I personally am using this on my AEM 6.5.5.0 instance and it's not specific to AEM as Cloud SDK.

 

@Component(service = {Servlet.class})
@SlingServletResourceTypes(
resourceTypes = "/apps/my/type",
methods = "GET",
extensions = "html",
selectors = "hello")
public class MyServlet extends SlingSafeMethodsServlet {

@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
...
}
}

 

Simple OSGi DS 1.2 annotations were used earlier and also can be used now only if we cannot use the above approach which I believe will never be a case 🙂

All the articles that are written so far are before DS 1.4 was released and it's never updated. But we should always refer the Sling document for updates.

 

@Component(service = {Servlet.class},
property = {
ServletResolverConstants.SLING_SERVLET_RESOURCE_TYPES + "=/apps/my/type"
ServletResolverConstants.SLING_SERVLET_METHODS + "=GET",
ServletResolverConstants.SLING_SERVLET_EXTENSIONS + "=html",
ServletResolverConstants.SLING_SERVLET_SELECTORS + "=hello",
}
)
public class MyServlet extends SlingSafeMethodsServlet {

@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
...
}
}

 

I will suggest that you should go with DS 1.4 version.

 

Hope this helps!

Thanks

Answers (0)