3 Replies Latest reply on Oct 27, 2010 3:33 AM by jaikiran pai

    JBoss 6 + Jax-RS + EJB

    Markus Buchner Newbie

      Hi

       

      How can I inject an EJB in a Resteasy enabled bean?     (JBoss 6.0 -  M5)

      The Rest Web Service works when I just return a static string!

       

      The EJB I want to inject:

       

      {code}

       

      @Stateless
      public class DAOService {
         
          @PersistenceContext
          private EntityManager em;

       

          /**
           * Select of a single entry by id
           * @param orderId
           * @return selected order
           */
          public JeeOrder getOrderByFind(long orderId) {
              JeeOrder order = em.find(JeeOrder.class, orderId);
              return order;
          }
      }

       

      {code}

       

      Here is my JAX-RS code:

       

      Trying to inject via @EJB: (Result: The Web Service works but giving me a NullPointerException for  daoService)

       

       

      {code}

      @RequestScoped
      @Path("/getorder")
      public class RestGetOrder {

          @EJB

          public DAOService daoService;

         
          @GET
          @Produces("text/plain")
          @Path("/byid/{orderid}")
          public String getOrderById(@PathParam("orderid") long orderID) {
                  JeeOrder order = daoService.getOrderByFind(orderID);
                 
                  String result = ""+
                      order.getOrderId()+", "+
                      order.getOrderNumber()+", "+
                      order.getOrderDate()+", "+
                      order.getOrderDeliveryDate()+", "+
                      order.getJeeCustomer().getCustFirstname()+" "+
                      order.getJeeCustomer().getCustLastname();
                 
                  return result;
          }

      {code}

       

       

      I also tried lookup via initial context (Is the jndi name wrong??? -> also tried "java:DAOService" and "java:DAOService/no-interface")

       

      The result is always an "Error NamingException DAOService not bound"

       

      {code}

       

      @RequestScoped


      @Path("/getorder")


      public class RestGetOrder {


         
          @GET


          @Produces("text/plain")


          @Path("/byid/{orderid}")


          public String getOrderById(@PathParam("orderid") long orderID) {


              try {


                  LocalDAOServiceInterface daoService =

      (LocalDAOServiceInterface) new InitialContext()


                  .lookup("java:global/JeeOntecDemo/DAOService");


                 
                  JeeOrder order = daoService.getOrderByFind(orderID);


                 
                  String result = ""+


                      order.getOrderId()+", "+


                      order.getOrderNumber()+", "+


                      order.getOrderDate()+", "+


                      order.getOrderDeliveryDate()+", "+


                      order.getJeeCustomer().getCustFirstname()+" "+


                      order.getJeeCustomer().getCustLastname();


                 


                  return result;


             
              } catch (NamingException ex) {


                      System.out.println("Error NamingException"+ex.getMessage());


              }   


              return "error";


          }

      {code}

       

      What do I have to do to get this working?

      -      Entries in web.xml?

      -      using @inject??

       

      Please give a hint!

        • 1. Re: JBoss 6 + Jax-RS + EJB
          jaikiran pai Master

          I'm not sure about the injection part, but for the lookup string, if you have packaged the EJB in a standalone jar (i.e. not in a .ear) then the lookup string should be: EJBName/no-interface. So for example, if you deployed the EJB in myapp.jar, then use DAOService/no-interface.

           

          If you are deploying it in a .ear, then use earname/EJBName/no-interface. Ex: myapp/DAOService/no-interface.

          • 2. Re: JBoss 6 + Jax-RS + EJB
            Markus Buchner Newbie

            I have everyting in a .war Project - so I think "java:DAOService/no-interface" should be right as JNDI name?

             

            Here is my web.xml:

             

            <?xml version="1.0" encoding="UTF-8"?>
            <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
            xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
              <description>JEE-Demo</description>
              <servlet>
                <servlet-name>Faces Servlet</servlet-name>
                <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
                <load-on-startup>1</load-on-startup>
              </servlet>
              <servlet-mapping>
                <servlet-name>Faces Servlet</servlet-name>
                <url-pattern>/faces/*</url-pattern>
              </servlet-mapping>
              <welcome-file-list>
                <welcome-file>index.html</welcome-file>
              </welcome-file-list>
            </web-app>

             

            I dont have any other configurations! Like a beans.xml or something like that - but I thought I dont need it - is that right??

            Or do I need other config files to get the injection part working.

             

            (All other components seperatly work well in my project just this injection doesnt work (JSF, JPA, Persistance, @WebServices))

             

            Help?

            • 3. Re: JBoss 6 + Jax-RS + EJB
              jaikiran pai Master

              Markus Buchner wrote:

               

              I have everyting in a .war Project - so I think "java:DAOService/no-interface" should be right as JNDI name?

               

              Don't use the java: prefix. Try warname/DAOService/no-interface.

               

              Markus Buchner wrote:

               


              I dont have any other configurations! Like a beans.xml or something like that - but I thought I dont need it - is that right??

              No, you don't need those for EJBs to work.