我是J2EE的新手,我有一个基于Rest的Web服务的jboss实现。我对jboss中的所有框架等都不是很熟悉。为了安全起见,我实现了一个SecurityInterceptor,我得到一个NullPointerException。 问题是kryptoManager.getKryptoInformation();因为kryptoManager为空而调用。
@EJB
KryptoManager kryptoManager;
@Override
public boolean accept(Class c, Method method) {
if (c == null || method == null) return false;
//class implementing rest service
return method.getName().equals("restService");
}
@Override
public ServerResponse preProcess(HttpRequest request, ResourceMethod method)
throws Failure, WebApplicationException {
ServerResponse response = null;
String key = kryptoManager.getKryptoInformation();
...
if (error) {
response = new ServerResponse(LOGINURL, 403, new Headers<Object>());
}
return response;
}
我还有一个Servlet和restService运行,我可以通过getKryptoInformation方法访问kryptoManager。 (我不必创建一些实例或其他) 为什么拦截器也不能通过KryptoBean访问数据库?是否有另一种可能性从后端的数据库访问或获取我的信息。 KryptoManager看起来像:
@Stateless
public class KryptoManager {
@EJB
private KryptoDAO kryptoDAO;
public KryptoBean getKryptoInformation() {
return kryptoDAO.getKryptoInformation();
}
}
KryptoDAO看起来像:
@Stateless
public class KryptoDAO {
@PersistenceContext(unitName = "db", type = PersistenceContextType.TRANSACTION)
private EntityManager entityManager;
public KryptoBean getKryptoInformation() {
Query query = entityManager.createQuery("SELECT k FROM KryptoBean k");
return (KryptoBean) query.getResultList().get(0);
}
}
Bean Class看起来像:
@XmlRootElement
@Table(name = "krypto")
@Entity(name = "KryptoBean")
public class KryptoBean {
@Id
@Column(name = "idkrypto")
private int idkrypto;
@Column (name = "hmackey")
private String hmackey;
public int getIdkrypto() {
return idkrypto;
}
public void setIdkrypto(int idkrypto) {
this.idkrypto = idkrypto;
}
public String getHmackey() {
return hmackey;
}
public void setHmackey(String hmackey) {
this.hmackey = hmackey;
}
}
JaxRsActivator看起来像:
@ApplicationPath( "/rest" )
public class JaxRsActivator extends Application
{
@Override
public Set<Class<?>> getClasses() {
// TODO Auto-generated method stub
return super.getClasses();
}
@Override
public Set<Object> getSingletons() {
// TODO Auto-generated method stub
return super.getSingletons();
}
}