我有一个来自CQ 5.6的jar,暴露了几个servlet。我将jar移动到新的CQ 6.3只是为了发现servlet没有运行(在Servlet Resolver上测试,它用com.day.cq.commons.servlets.NonExistingDispatcherServlet
回答 - 等于404)。
我已经做了几个清单来缩小这个问题:
apps
目录下,其中包含在Apache Sling Servlet / Script Resolver和Error Handler Execution Paths
中。是否有任何缺少的清单让servlet处理sling.servlet.paths
属性中定义的特定路径?
抱歉这个菜鸟问题。提前谢谢。
修改
添加了粗略的代码 - 我认为您不会对doPost
中发生的事情感兴趣。因为它与将servlet注册到AEM 6.3无关。
package com.test.something;
import java.util.Iterator;
import java.io.*;
import javax.jcr.*;
import javax.servlet.ServletException;
import java.util.*;
import java.util.Date;
import java.text.*;
import org.apache.jackrabbit.api.security.user.Group;
import org.apache.jackrabbit.api.security.user.User;
import org.apache.jackrabbit.api.security.user.UserManager;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.jackrabbit.api.*;
import java.rmi.ServerException;
import java.security.Principal;
import org.apache.jackrabbit.api.security.principal.PrincipalManager;
import org.apache.jackrabbit.api.security.user.AuthorizableExistsException;
import java.security.*;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.Mac;
import org.apache.commons.codec.binary.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.jcr.api.SlingRepository;
@Component(immediate = true)
@Service
@Properties({
@Property(name = "sling.servlet.methods", value = "POST"),
@Property(name = "sling.servlet.paths", value = { "/content/test" }),
@Property(name = "service.description", value = "Test Servlet")
})
public class TestServlet extends SlingAllMethodsServlet {
@Reference
private SlingRepository repository;
protected final Logger log = LoggerFactory.getLogger("SSOLogger");
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
throws ServerException, IOException {
try {
this.doPost(request, response);
} catch (ServletException e) {
//do some logging
}
}
@Override
protected void doPost(SlingHttpServletRequest request,
SlingHttpServletResponse response)
throws ServletException, IOException {
try {
//do something
} catch (RepositoryException ex) {
//do some logging
}
}
}
是的,这个servlet已在OSGi Components中注册。它正确地陈述了属性。
编辑:缩小罪魁祸首
@Reference
private SlingRepository repository;
似乎没有正确加载..很奇怪。它导致组件处于 Satisfied 状态而不是 Active 。
编辑:检查CQ中的OSGi服务,注意到{5.6}中的org.apache.sling.jcr.api.SlingRepository
由com.day.crx.sling.server
提供,而在CQ 6.3中则由com.adobe.granite.repository
提供。不确定是不是原因。
答案 0 :(得分:0)
Servlet本质上是OSGI组件,因此您应该能够在OSGI组件Web控制台中看到您的servlet:/system/console/components
您的servlet可能已被注册但由于某种原因而被禁用,或者由于您的编写方式存在问题而无法注册。
请提供您的servlet代码,以便我们进一步提供帮助
答案 1 :(得分:0)
稍微弄乱并检查日志。显然问题在于maven scr插件没有生成必要的绑定(在@Reference
)方法中绑定和取消绑定。基本上,我需要严格定义方法,如:
public class TestServlet extends SlingAllMethodsServlet {
@Reference(bind = "bindRepository", unbind = "unbindRepository")
private SlingRepository repository;
protected void bindRepository(SlingRepository repository){
this.repository = repository;
}
protected void unbindRepository(SlingRepository repository){
if(this.repository == repository){
this.repository = null;
}
}
}
我们需要检查一些项目是否服务处于满意状态:
slingRepository.loginAdministrative(null)
。@Activate
表示)?我知道清单不完整,我还在学习这个OSGi的东西。虽然有点不清楚(没有傻瓜文档),但这很有意思。