user489041
user489041

Reputation: 28312

Can not use EJB's in a Restful Webservice

I have a pretty simple class that is a Restful web service. I want to use a EJB in the web service. I used the @EJB annotation to inject my EJB. The only problem was, the EJB was always null. I then made my web service a Stateless EJB itself. This fixed the null problem. However, when I try to call any function on my EJB, I get the following error:

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

}

I had this working before. Do I need to do something special in order to access EJB's from a Restful web service? Any other ideas?

EDIT: Here is the code for 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;
}


}

Upvotes: 1

Views: 899

Answers (1)

kasavbere
kasavbere

Reputation: 6003

Is it safe to assume that your BaseDao has the call to EntityManager like so:

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

Also you don't need @LocalBean on CasperLoggingDao. And you also need @Stateless on BaseDao

Upvotes: 0

Related Questions