where ware the "new" annotations for servlets? | Community
Skip to main content
New Participant
April 18, 2021
Solved

where ware the "new" annotations for servlets?

  • April 18, 2021
  • 1 reply
  • 655 views

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:

 

@8220494(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?

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 Asutosh_Jena_

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

1 reply

Asutosh_Jena_
Asutosh_Jena_Accepted solution
New Participant
April 18, 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