8 Replies Latest reply on Aug 18, 2008 3:14 AM by jake j

    best way to expose ejb3 as web service?

    jake j Newbie

      I have an ejb3 component with a number of stateless and entity beans. What's the best and easiest way to expose the stateless beans as web services? Which method will require the least changes to be made to the beans themselves?

        • 1. Re: best way to expose ejb3 as web service?
          Peter Johnson Master

          Add an @WebService annotation to the EJB3 class, and an @WebMethod annotation to each method you want to export.

          • 2. Re: best way to expose ejb3 as web service?
            jake j Newbie

            Thanks, that worked!

            But I have got another problem now. To avoid changing the existing stateless beans, for each, I have created a wrapper sort of stateless bean having the web service annotations and calling the methods in the existing beans. (This had to be done since the client app can't recognize many of the objects taken as parameters by methods of the existing beans.) But calling a method from the wrapper bean having a createQuery statement throws a NullPointerException. Any idea on why this might be?

            • 3. Re: best way to expose ejb3 as web service?
              jaikiran pai Master

              Post the code and the entire exception stacktrace.

              • 4. Re: best way to expose ejb3 as web service?
                jake j Newbie

                Method in the stateless bean:



                @SuppressWarnings("unchecked")
                 public User check(String login,String mdp)
                 {
                 Collection<User> users = new HashSet<User>();
                
                 users = em.createQuery( "SELECT u " +
                 "FROM User u " +
                 "where user_login_id='" + login + "' and pass='"+ mdp +"'").getResultList();
                 User u = null;
                 for (Iterator<User> iterator = users.iterator(); iterator.hasNext();)
                 u = iterator.next();
                 return u;
                 }


                Method in the exposed stateless bean trying to call the above method :

                @SuppressWarnings("unchecked")
                 public Utilisateur check(String login,String mdp)
                 {
                 Collection<Utilisateur> users = new HashSet<Utilisateur>();
                
                 System.out.println("ENTERED USER CHECK "+login+mdp);
                
                 Object random = em.createQuery("Select count(u)"+"from Utilisateur u").getSingleResult();
                
                 users = em.createQuery( "SELECT u " +
                 "FROM Utilisateur u " +
                 "where user_login_id='" + login + "' and motdepasse='"+ mdp +"'").getResultList();
                 Utilisateur u = null;
                 for (Iterator<Utilisateur> iterator = users.iterator(); iterator.hasNext();)
                 u = iterator.next();
                 return u;
                 }



                Stack trace:

                Caused by: java.lang.NullPointerException
                at com.session.impl.UserManagerBean.check(UserManagerBean.java:122)
                at com.session.impl.UserBean.check(UserBean.java:37)
                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                at java.lang.reflect.Method.invoke(Method.java:585)
                at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:112)
                at org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:166)
                at org.jboss.ejb3.interceptor.EJB3InterceptorsInterceptor.invoke(EJB3InterceptorsInterceptor.java:63)
                at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
                at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntityManagerInterceptor.java:54)
                at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
                at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:47)
                at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
                at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:79)
                ... 39 more


                • 5. Re: best way to expose ejb3 as web service?
                  jake j Newbie

                  I posted the wrong code in the 2nd code section in the above post. Below is the correct one:

                  @WebMethod
                   public String checkLogin(String id, String pass)
                   {
                   User u = new UserManagerBean().check(id,pass);
                  
                   if(u==null)
                   {
                   return "Invalid Username / Password";
                   }
                   else
                   {
                   return "Success";
                   }
                   return u;
                   }




                  • 6. Re: best way to expose ejb3 as web service?
                    jaikiran pai Master

                     

                    "jaki" wrote:

                     User u = new UserManagerBean().check(id,pass);
                    
                    




                    I believe UserManagerBean is a EJB. You should never be instantiating an bean, in your code. Instead you should be "looking up" the bean. There was a similar discussion about this sometime back at http://www.jboss.com/index.html?module=bb&op=viewtopic&t=136213. See my reply dated Mon May 26, 2008 11:02 AM in that thread. Another similar thread can be found at

                    http://www.jboss.com/index.html?module=bb&op=viewtopic&t=137590

                    • 7. Re: best way to expose ejb3 as web service?
                      jake j Newbie

                      That worked! Much thanks

                      • 8. Re: best way to expose ejb3 as web service?
                        jake j Newbie

                        Hi again,

                        Is it not possible to return a map/hashmap from the ejb web service? I'm using an axis client for that as below

                        Call call = (Call) service.createCall();
                         call.setTargetEndpointAddress( new java.net.URL(endpoint) );
                         call.setOperationName(new QName("http://session.um.com/", "getAuc") );
                        
                        
                         call.addParameter("arg0",
                         org.apache.axis.Constants.XSD_INT,
                         javax.xml.rpc.ParameterMode.IN);
                        
                        
                         call.setReturnClass(HashMap.class);
                         HashMap ret = (HashMap) call.invoke( new Object[] {"1"}


                        Thought it throws no error, the returned Map never has any contents in them. Also, if instead of setReturnClass I try to use setReturnType(org.apache.axis.Constants.XSD_ANY) it gives me the below error:

                        could not find deserializer for type {http://www.w3.org/2001/XMLSchema}any