gpt4 book ai didi

java - 无法在 Restful Web 服务中使用 EJB

转载 作者:行者123 更新时间:2023-12-01 05:12:28 25 4
gpt4 key购买 nike

我有一个非常简单的类,它是一个 Restful Web 服务。我想在 Web 服务中使用 EJB。我使用 @EJB 注释来注入(inject)我的 EJB。唯一的问题是,EJB 始终为空。然后我将我的 Web 服务本身设为无状态 EJB。这解决了空问题。但是,当我尝试调用 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...

This is my class. Notice all I am doing is calling the 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;
}


}

最佳答案

可以安全地假设您的 BaseDao 调用 EntityManager 如下:

@PersistenceContext(unitName = "somePU") 
protected EntityManager entityManager;

此外,您不需要 CasperLoggingDao 上的 @LocalBean。并且您还需要 BaseDao 上的 @Stateless

关于java - 无法在 Restful Web 服务中使用 EJB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11854720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com