我有一个非常简单的类,它是一个Restful Web服务。我想在Web服务中使用EJB。我使用@EJB注释来注入我的EJB。唯一的问题是,EJB始终为null。然后我将我的Web服务本身作为无状态EJB。这解决了null问题。但是,当我尝试在EJB上调用任何函数时,我收到以下错误:
EVERE: EJB5070: Exception creating stateless session bean : [CasperLoggingDao] WARNING: EJB5184:A system exception occurred during an invocation on EJB CasperLoggingDao, method: public boolean com.dv.model.dao.CasperLoggingDao.log(com.dv.model.entity.CasperLogMessage) WARNING: javax.ejb.EJBException: javax.ejb.EJBException: javax.ejb.CreateException: Could not create stateless EJB at com.sun.ejb.containers.StatelessSessionContainer._getContext(StatelessSessionContainer.java:454) at com.sun.ejb.containers.BaseContainer.getContext(BaseContainer.java:2547) at com.sun.ejb.containers.BaseContainer.preInvoke(BaseContainer.java:1899) at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:212) at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:88) at $Proxy319.log(Unknown Source) at com.dv.model.dao.__EJB31_Generated__CasperLoggingDao__Intf____Bean__.log(Unknown Source) at com.dv.ws.casper.logging.CasperLoggingResource.putXml(CasperLoggingResource.java:64) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1052) at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1124) at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:5388) at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:619) at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800) at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:571) at org.jboss.weld.ejb.SessionBeanInterceptor.aroundInvoke(SessionBeanInterceptor.java:42) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(Int ...
这是我的班级。注意我正在做的就是调用log():
@Stateless
@Path("casper/logging")
public class CasperLoggingResource {
private static final Logger logger = Logger.getLogger(CasperLoggingResource.class.getName());
@Context
private UriInfo context;
@EJB
private CasperLoggingDao casperLogger;
/**
* Creates a new instance of GenericResource
*/
public CasperLoggingResource() {
}
/**
* PUT method for updating or creating an instance of GenericResource
* @param content representation for the resource
* @return an HTTP response with content of the updated or created resource.
*/
@PUT
@Consumes("application/xml")
public void putXml(String content) {
try
{
CasperLogMessage message = CasperLogMessageConverter.convertXMLToEntity(content);
casperLogger.log(message);
}
catch(Exception e)
{
logger.log(Level.SEVERE, "Could not execute", e);
}
}
}
我之前有过这个工作。我是否需要做一些特殊的事情才能从Restful Web服务访问EJB?还有其他想法吗?
编辑:这是CasperLoggingDao的代码
@Stateless
@LocalBean
public class CasperLoggingDao extends BaseDao<CasperLogMessage> {
private static final Logger logger = Logger.getLogger(CasperLoggingDao.class.getName());
public CasperLoggingDao()
{
super(CasperLogMessage.class);
}
public CasperLoggingDao(EntityManager em)
{
super(CasperLogMessage.class, em);
}
public boolean log(CasperLogMessage casperLogMessage)
{
return create(casperLogMessage) != null;
}
}
答案 0 :(得分:0)
您可以安全地假设BaseDao
有EntityManager
这样的电话:
@PersistenceContext(unitName = "somePU")
protected EntityManager entityManager;
此外,您@LocalBean
上不需要CasperLoggingDao
。您还需要@Stateless
BaseDao