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?
Solved! Go to Solution.
Views
Replies
Total Likes
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
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
Views
Likes
Replies