Tien Nguyen
Tien Nguyen

Reputation: 4358

cq5 can't create servlet resource

I create a servlet in my CQ5 application:

import java.io.IOException;

import javax.servlet.ServletException;

import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;

@SlingServlet(
        label = "Example Servlet",
        paths = {"/bin/project/signin"},
        methods = {"GET"},
        extensions = {"html"},
        metatype = false
)
public class SignInServlet extends SlingAllMethodsServlet  {

    private static final long serialVersionUID = 796802690004962223L;

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

    @Override
    protected void doPost(SlingHttpServletRequest request,
            SlingHttpServletResponse response) throws ServletException,
            IOException {
        response.setContentType("text/plain");
        response.getOutputStream().print("Sigin Servlet");
    }
}

i try to call it via rest-client but it return:

No resource found for url: http://localhost:4502/bin/project/signin

I also open Sling Resource Resolver at Felix side to test if it available. But i received:

NonExistingResource, path=/bin/project/signin

/bin/ already config in Apache Sling Servlet/Script Resolver and Error Handler at Fellix

Upvotes: 0

Views: 1253

Answers (2)

anotherdave
anotherdave

Reputation: 6744

Two ways to help debug this type of situation —

  1. The ServletResolver will allow you to check which Servlet a given GET or POST request will resolve against.
  2. If you find the request you made that generated the 404 in the Recent Requests tab, it should tell you exactly what properties it has found by Sling when trying to resolve it.

E.g. in your case, I presume the 404 is giving something like:

LOG Resource Path Info: SlingRequestPathInfo: \
path='/bin/project/signin', \
selectorString='null', \
extension='null', \
suffix='null'

Comparing this against the settings in your annotation, there's an extension='null' here that wouldn't match against your servlet — which is binding only against a 'html' extension, as Tomek rightly says above.

Upvotes: 2

Tomek Rękawek
Tomek Rękawek

Reputation: 9304

In the @SlingServlet annotation you've declared that this servlet supports only requests with .html extension, so you should hit following URL:

http://localhost:4502/bin/project/signin.html

If you don't want to use the extension, remove appropriate parameter from the servlet annotation.

Upvotes: 2

Related Questions