ha so
2013-06-04 11:52:09 UTC
ha so [https://community.jboss.org/people/haso] created the discussion
"Cant inject Bean class into Restfull WebService (JAX-RS)"
To view the discussion, visit: https://community.jboss.org/message/821240#821240
--------------------------------------------------------------
I'm trying to save data acquired by Rest web service to database using hibernate/persistence.
In one of my web modules i implemented that service. Database ejb connector is placed in EJB module. They are parts of EAR application.
Every time when i call pb.addDevice() im getting java.lang.NullPointerException when puting proper url with params in browser(worked till i wanted to save it to Database). Can't find what is wrong with it. I'm using jboss 6.1.0 Final.
PS. when i changed from
   @EJB
   PersistenceBean pb;
to
   PersistenceBean pb = new PersistenceBean();
i got null pointer on EntityManager em = emf.createEntityManager();
code:
   @Stateless
   @Path("/RestService")
   public class RestPush {
   @EJB
   PersistenceBean pb;
       @GET
         @Path("/RegisterDevice")
       public void registerDevice(
           @QueryParam("deviceId") String deviceId){
           Device d = new Device(true);
           d.setId = deviceId;
           pb.addDevice(d);
       }
   }
and EJB class:
   @Stateless(mappedName = "PersistenceBean")
   public class PersistenceBean {
   @PersistenceUnit(unitName = "PersistentUnitName")
   EntityManagerFactory emf;
   private void persist(Object o, EntityManager entityManager) {
       try {
           entityManager.persist(o);
       } catch (Exception e) {
           logger.severe("Error writing to DB: " + e);
           logger.severe("" + e.fillInStackTrace());
       }
   }
     public void addDevice(Device d) {
       try {
           EntityManager em = emf.createEntityManager();
       if (persist(device, em)) {
               logger.info("Device with id : " + device.getId()
                       + " has been added ");
   } else {
               logger.info("Failed to add device with id: " + device.getId());
   } catch (Exception e) {
           logger.severe("PersistenceBean: Could not save device.");
           e.printStackTrace();
   }
   }
--------------------------------------------------------------
Reply to this message by going to Community
[https://community.jboss.org/message/821240#821240]
Start a new discussion in EJB3 at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2029]
"Cant inject Bean class into Restfull WebService (JAX-RS)"
To view the discussion, visit: https://community.jboss.org/message/821240#821240
--------------------------------------------------------------
I'm trying to save data acquired by Rest web service to database using hibernate/persistence.
In one of my web modules i implemented that service. Database ejb connector is placed in EJB module. They are parts of EAR application.
Every time when i call pb.addDevice() im getting java.lang.NullPointerException when puting proper url with params in browser(worked till i wanted to save it to Database). Can't find what is wrong with it. I'm using jboss 6.1.0 Final.
PS. when i changed from
   @EJB
   PersistenceBean pb;
to
   PersistenceBean pb = new PersistenceBean();
i got null pointer on EntityManager em = emf.createEntityManager();
code:
   @Stateless
   @Path("/RestService")
   public class RestPush {
   @EJB
   PersistenceBean pb;
       @GET
         @Path("/RegisterDevice")
       public void registerDevice(
           @QueryParam("deviceId") String deviceId){
           Device d = new Device(true);
           d.setId = deviceId;
           pb.addDevice(d);
       }
   }
and EJB class:
   @Stateless(mappedName = "PersistenceBean")
   public class PersistenceBean {
   @PersistenceUnit(unitName = "PersistentUnitName")
   EntityManagerFactory emf;
   private void persist(Object o, EntityManager entityManager) {
       try {
           entityManager.persist(o);
       } catch (Exception e) {
           logger.severe("Error writing to DB: " + e);
           logger.severe("" + e.fillInStackTrace());
       }
   }
     public void addDevice(Device d) {
       try {
           EntityManager em = emf.createEntityManager();
       if (persist(device, em)) {
               logger.info("Device with id : " + device.getId()
                       + " has been added ");
   } else {
               logger.info("Failed to add device with id: " + device.getId());
   } catch (Exception e) {
           logger.severe("PersistenceBean: Could not save device.");
           e.printStackTrace();
   }
   }
--------------------------------------------------------------
Reply to this message by going to Community
[https://community.jboss.org/message/821240#821240]
Start a new discussion in EJB3 at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2029]