0

I have a problem with injecting EJB inside of a REST service (using jersey on glassfish 3.2 server) and I'm puzzled.

I have an EJB interface declared as:

import javax.ejb.Local;

@Local
public interface TestServiceLocal {

    public String getText();
}

and the class bean that implements it:

import javax.ejb.Local;
import javax.ejb.Stateless;

/**
 * Session Bean implementation class TestService
 */
@Stateless
@Local(TestServiceLocal.class)
public class TestService implements Serializable, TestServiceLocal {

    private static final long serialVersionUID = 1L;

    /**
     * Default constructor.
     */
    public TestService() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public String getText() {
        return this.getClass().getName();
    }

}

The REST service looks like:

@Path("/service")
@Stateless
public class TestRestService {

    @EJB(beanName="TestService")
    private TestServiceLocal testService;

    public TestRestService () {
    }

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    @Path("/events")
    public String getText() {
        return testService.getText();
    }
}

The problem is that when the REST service is called the bean cannot be created:

SEVERE: EJB5070: Exception creating stateless session bean : [TestRestService]
WARNING: EJB5184:A system exception occurred during an invocation on EJB TestRestService, method: public java.lang.String TestRestService.getText()
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 com.sun.proxy.$Proxy839.getText(Unknown Source)

I had already took a look at the answers posted here but none of them seemed to work for me. Any help will be appreciated. Thank you!

PS: I forgot to mentioned (don't know if it's relevant). My project is created under eclipse Juno as Dynamic Web Project.

Community
  • 1
  • 1
  • Hi, I tried out your code and it works perfectly. I'm not sure why yours isn't working. – greenkode May 03 '13 at 19:15
  • Is your EJB in a different application ? (war, ear..) Because you have only local interface defined. EJB won't be visible outside deployed application. Means, cross application call will fail. – Ravi Trivedi May 05 '13 at 01:32
  • Hi Ravi, tanks for your response. Yes, I have an EAR which contains a business layer (i.e. an eclipse EJB project) where the EJB is declared along with its interface. The EJB is used in web layer (i.e. an eclipse dynamic web project). But the business layer is added as a referenced project for web tire, added to the build path and also marked for publishing/exporting in the 'Order and export' tab. –  May 07 '13 at 05:45

0 Answers0