cq5无法创建servlet资源

时间:2014-02-11 07:18:28

标签: java servlets osgi cq5 sling

我在CQ5应用程序中创建了一个servlet:

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");
    }
}

我尝试通过rest-client调用它,但它返回:

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

我还在Felix一侧打开Sling Resource Resolver来测试它是否可用。但我收到了:

NonExistingResource, path=/bin/project/signin

/bin/已在Apache Sling Servlet /脚本解析器和Fellix的错误处理程序中配置

2 个答案:

答案 0 :(得分:2)

@SlingServlet注释中,您声明此servlet仅支持.html扩展名的请求,因此您应该点击以下网址:

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

如果您不想使用扩展名,请从servlet注释中删除相应的参数。

答案 1 :(得分:2)

两种帮助调试此类情况的方法 -

  1. ServletResolver将允许您检查给定的GET或POST请求将解决哪个Servlet。
  2. 如果您发现在 Recent Requests 标签中生成了404的请求,它应该告诉您Sling在尝试解决它时确切找到了哪些属性。
  3. E.g。在你的情况下,我认为404正在给出类似的东西:

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

    将此与注释中的设置进行比较,这里有一个 extension ='null',它与您的servlet不匹配 - 仅绑定'html'扩展,正如Tomek正确地说的那样。