1
I’m making a requisition for "/counter/{id}" by Postman and when the code arrives in notifiedAAO.counter(id), I get the error message saying that notifiedDAO is null.
PS: I’m using Wildfly 11 with Eclipse Oxygen
Controller
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.enterprise.context.RequestScoped;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
@Path("ejb/notificacao")
@Stateless
@LocalBean
public class NotificacaoControladorEJB {
SmartJava smartjava = new SmartJava();
@EJB
NotificacaoDAOEJB notificacaoDAO;
@GET
@Produces("application/json; charset=UTF-8")
@Path("/contador/{id}")
public Notificacao contadorGet(@PathParam("id") int id) {
long quantidade;
try {
quantidade = notificacaoDAO.contador(id);
return new Notificacao(quantidade);
} catch(Exception e) {
quantidade = 0;
System.err.println(smartjava.getFullStackTrace(e));
return new Notificacao(quantidade);
}
}
DAO
import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Stateless
@LocalBean
public class NotificacaoDAOEJB {
//private EntityManagerFactory entityManagerFactory;
@PersistenceContext
private EntityManager entityManager;
SmartJava sj = new SmartJava();
public Notificacao Salvar(Notificacao notificacao) {
try {
this.entityManager.getTransaction().begin();
this.entityManager.persist(notificacao);
this.entityManager.getTransaction().commit();
} catch (Exception e) {
System.out.println(sj.getFullStackTrace(e));
} finally {
//this.entityManager.close();
}
return notificacao;
}
public void Alterar(Notificacao notificacao){
this.entityManager.getTransaction().begin();
this.entityManager.merge(notificacao);
this.entityManager.getTransaction().commit();
//this.entityManager.close();
}
@SuppressWarnings("unchecked")
public List<Notificacao> Listar(){
return this.entityManager.createQuery("SELECT a FROM Notificacao a ORDER BY a.dtcad").getResultList();
}
public Notificacao GetNotificacao(int nrseq) {
return this.entityManager.find(Notificacao.class, nrseq);
}
@SuppressWarnings("unchecked")
public long contador(int nrsequsuario) {
try {
return (long) this.entityManager.createQuery("SELECT COUNT(a) FROM Notificacao a "
+ "WHERE a.visualizado = false AND a.useralvo.nrseq = :usuario ORDER BY a.dtcad")
.setParameter("usuario", nrsequsuario).getSingleResult();
} catch(Exception e) {
System.err.println(sj.getFullStackTrace(e));
return 0;
}
}
}
Can you see if your ejb is successfully installed? This appears in the console logs when Wildfly is started (with the package) or when you add the project.
– escapistabr
I believe Voce does not need this with the @Localbean annotation in the Notificaodaoejb class.
– Cleibson