CQ5.3 Servlet not available (and configuring servlet lattice paths (5.3 v 5.6))
Historically I only used CQ 5.6, but now I am working on a project using 5.3 and I cannot get the servlet registered and available. When I try to get into the servlet (with and without extension), I get the following INFO line in the error log:
GET /bin/TestServlet.json HTTP/1.1] org.apache.sling.engine.impl.SlingMainServlet service: Resource /bin/TestServlet.json not found
I can create a package containing the servlet, deploy it using the maven-sling-plugin module, and the bundle ends up in the console as "active", but the servlet is not available. I am using the following in my class file to define the servlet:
@SlingServlet(paths = {"/bin/TestServlet"}, methods = {"GET"}, extensions = {"json"}, metatype = true)
@Properties({
@Property(name = "service.name", value = "com.something.servlets.TestServlet", propertyPrivate = true),
@Property(name = "service.description", value= "Test servlet I am trying to geet working", propertyPrivate = true),
@Property(name = "service.vendor", value = "something", propertyPrivate = true)
})
Please note that I also tried this alternative using annotations @Component(.......)
and@Service(value=Servlet.class)
After looking for a config based issue (thinking / bit was locked), I went back to 5.6.1 to compare the config and noticed the config for Apache Sling Servlet / Script Resolver and Error Handler differs from 5.3 to 5.6.
In 5.6 you can set up servletresolver.paths, which (if I understand correctly) opens paths for execution, but this is not in 5.3
So:
- Does the config for servletresolver.paths have in 5.3?
- Can I register such a servlet in 5.3?
- If the answer to 2 is no, can you point me to an example / tutorial on how to do this in 5.3 (The docs and tutorials are thin on earth for this version)?
Thank you in advance
EDIT (permission) : Seems to be up to the version in the POM (and possibly with a plugin). Maybe it was the only culprit, but I just don't have time to try all the permutations. If I have time to try, I'll add an edit.
Successful versions: maven-bundle-plugin - v2.4.0 maven-scr-plugin - v1.13.0 maven-sling-plugin - v2.1.0
source to share
in CQ5.3 you can use a "JCR resolver" to check the mappings of the servlet. Go to
http://localhost:4502/system/console/jcrresolver
And enter the path to your servlet in 'Configuration Test'. eg.
http://localhost:4502/bin/TestServlet
allow result when matched against your servlet eg
ServletResource, servlet=com.test.impl.HelloJSONServlet, path=/bin/TestServlet
There is also a code snippet that works in CQ5.3
@Component(immediate = true, metatype = false, label = "Hello JSON Servlet")
@Service(serviceFactory = false, value = javax.servlet.Servlet.class)
@Properties(value = {
@org.apache.felix.scr.annotations.Property(name = "sling.servlet.methods", value = { "GET" }),
@org.apache.felix.scr.annotations.Property(name = "sling.servlet.extensions", value = { "json" }),
@org.apache.felix.scr.annotations.Property(name = "sling.servlet.paths", value = {"/bin/TestServlet"})
})
public class HelloJSONServlet extends SlingAllMethodsServlet {
protected void doGet(SlingHttpServletRequest request,
SlingHttpServletResponse response) throws ServletException,
IOException {
String username = request.getParameter("username");
response.setContentType("application/json");
response.getWriter().write("{ \"hello\" : \""+username+"\" }");
}
}
source to share