Search code examples
jax-rsejbwildfly

JAX-RS @EJB injection gives NullPointerException


I'm trying to deploy a war file to run on WildFly Swarm. While doing a GET request, a NullPointerException occurs because an injection fails and the reference is, obviously, null.

SomeDao.java

import java.util.List;
import java.util.UUID;

import javax.ejb.Local;

@Local
public interface SomeDao {
    public List<MyEntity> listAll();
    public void store(MyEntity entity);
}

SpecializedDao.java

import java.util.List;
import java.util.UUID;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless
public class SpecializedDao implements SomeDao {
    @PersistenceContext(unitName="primary")
    protected EntityManager entityManager;

    public SpecializedDao() {}

    @Override
    public List<MyEntity> listAll() {
        return this.entityManager
                .createQuery("SELECT entity FROM MyEntity entity", MyEntity.class)
                .getResultList();
    }

    @Override
    public void store(MyEntity entity) {
        entityManager.getTransaction().begin();
        entityManager.persist(entity);
        entityManager.getTransaction().commit();
    }
}

Then, there's the endpoint where I need to inject a SpecializedDao instance.

MyEndpoint.java

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Stateless
@Path("/something")
public class MyEndpoint {

    @EJB
    private SomeDao dao;

    @GET
    @Path("/test")
    @Produces({MediaType.APPLICATION_JSON})
    public Response test() {
        MyEntity testEntity = new MyEntity("something", "something");

        dao.store(testEntity);

        return Response.ok("All done!").build();
    }


}

beans.xml

<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
       version="1.2" bean-discovery-mode="annotated">
</beans>

The NullPointerException is launched at dao.store(testEntity), because dao references a null object. I'm pretty sure the persistence.xml file is correct because the EntityManager works on another test case, so I think the problem is at injection.

Where did I do something wrong?


Solution

  • Other things you can check.

    1. Is your beans.xml located in the correct folder (ie in WEB-INF in case of a webapp/war) ?
    2. Did you include the Swarm CDI fraction/dependency (org.wildfly.swarm:cdi) ?
    3. I'm not a Swarm expert but it may be that the CDI fraction only works when beans are injected "à la JSR-299" that is using java.inject.@Inject (rather than using @EJB).

          @Inject
          private SomeDao dao;
      
    4. Ultimately: try with bean-discovery-mode="all" in beans.xml (...even though "annotated" seems correct)