adutra
adutra

Reputation: 358

EJB Stateless session bean is null

I'm trying to access an EJB Stateless session bean from a local service. But when I call a method that is located on the bean, I get a NPE because the stateless bean is null.

Here is the code:

The sateless bean:

@Startup
@Stateless(name = "LoginBean")
@LocalBean
public class LoginBean {


    public List<Long> getItemsForClient(String clientId, Long itemId) {
        System.out.println("clientID: " + clientId);
        System.out.println("itemID: " + itemId);

        List<Long> ret = new ArrayList<Long>();
        ret.add((long) 123456);
        ret.add((long) 123457);
        ret.add((long) 123458);
        ret.add((long) 123459);
        return ret;

    }

    }

The service:

@Stateless
@Path("/ctofservice")
public class CtoFService {

    @EJB
    LoginBean loginBean;

    public CtoFService() {

    }

    @GET
    @Produces("text/plain")
    @Path("test")
    public String convertCtoF() {

        Long l = (long) 123456;
        List<Long> servicesForClient = loginBean.getItemsForClient("cliID", l);
        return itemsForClient.toString();


    }

And the ApplicationConfig:

@ApplicationPath("/")
public class ApplicationConfig extends Application {

    @SuppressWarnings("unchecked")
    @Override
    public Set<Class<?>> getClasses() {

        Set<Class<?>> resources = new java.util.HashSet<Class<?>>();
        addRestResourceClasses(resources);
        return resources;
    }

    private void addRestResourceClasses(Set<Class<?>> resources) {
        resources.add(CtoFService.class);    
    }
}

I've been trying for a while and looking for possible solutions, but nothing came up.

I'm using JBoss AS 7.1 and RESTEasy that cames with it.

When the bean should get instantiated?

Thanks.

Upvotes: 1

Views: 2133

Answers (1)

adutra
adutra

Reputation: 358

I solved it by adding beans.xml file, It wasn´t present at the moment that I created the project, and I came across to that file searching for a solution after hours.

So I placed the file in WEB-INF directory

The file contains:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
</beans>

And the problem is solved, I can access the beans through the webService.

Thanks for trying to help.

Upvotes: 2

Related Questions