5 Replies Latest reply on Nov 5, 2009 8:35 PM by ybxiang.china

    Problem of polymorphism Web Service in EJB

    ybxiang.china

      Dear friends,
      I can use pure polymorphism Web Service in Glassfish and JBoss correctly.
      But, after annotated with @Stateless, my codes does NOT work.
      Namely, i can not make my code work in EJB3 environment.

      My environment:
      JDK, lates.
      jboss-5.1.0.GA-jdk6
      Windows XP.

      My java files:

      package com.ybxiang.ejbws;
      
      import javax.xml.bind.annotation.XmlSeeAlso;
      
      @XmlSeeAlso({Human.class})
      public class Animal implements java.io.Serializable{
       private String name;
       private Long age;
       public String getName() {
       return name;
       }
       public void setName(String name) {
       this.name = name;
       }
      
       public Long getAge() {
       return age;
       }
       public void setAge(Long age) {
       this.age = age;
       }
      
       public String toString(){
       return "Animal-->name:"+this.getName()+",age:"+this.getAge();
       }
      }
      
      
      
      
      
      
      package com.ybxiang.ejbws;
      
      public class Human extends Animal implements java.io.Serializable{
       private String email;
      
       public void setEmail(String email) {
       this.email = email;
       }
      
       public String getEmail() {
       return email;
       }
      
       public String toString(){
       return "Human-->name:"+this.getName()+",age:"+this.getAge()+","+"email:"+email;
       }
      
      }
      
      
      
      
      
      
      
      package com.ybxiang.ejbws;
      
      import java.util.ArrayList;
      import java.util.Iterator;
      
      import javax.jws.WebMethod;
      
      public interface EJBWebServiceInterface {
       public void passParameter1(String s);
       public void passParameter2(Animal a);
       public void passParameter3(Human h);
       public void passParameter4(ArrayList<Animal> lst);
       public ArrayList<? extends Animal> returnSomething1(int type);
      }
      
      
      
      
      
      package com.ybxiang.ejbws;
      
      import java.util.ArrayList;
      import java.util.Iterator;
      
      import javax.ejb.Stateless;
      import javax.jws.WebMethod;
      import javax.jws.WebService;
      import javax.xml.bind.annotation.XmlSeeAlso;
      
      import javax.jws.soap.SOAPBinding;
      
      @Stateless
      @WebService
      //@**XmlSeeAlso({Animal.class, Human.class})// Here i tried too!
      public class EJBWebServiceBean implements EJBWebServiceInterface{
       @WebMethod
       public void passParameter1(String s){
       System.out.println("------------String is got:"+s);
       }
       @WebMethod
       public void passParameter2(Animal a){
       System.out.println("------------Animal is got:"+a);
       if(a instanceof Human){
       System.out.println("************* It's realy human!!! *************");
       }
       }
       @WebMethod
       public void passParameter3(Human h){
       System.out.println("------------Human is got, name:"+h.getName()+", email:"+h.getEmail());
       }
      
       @WebMethod
       public void passParameter4(ArrayList<Animal> lst){
       System.out.println("------------ArrayList<Animal> is got:");
       if(lst!=null){
       Iterator<Animal> it = lst.iterator();
       Animal a = it.next();
       if(a instanceof Human){
       System.out.println("One human is got:"+((Human)a));
       }else{
       System.out.println("One Animal is got:"+a);
       }
       }else{
       System.out.println("------------List is null.");
       }
       }
      
       @WebMethod
       public ArrayList<? extends Animal> returnSomething1(int type){
       if(type==1){
       ArrayList<Animal> aa = new ArrayList<Animal>();
       {
       Animal animal1 = new Animal();
       animal1.setName("Animal.1");
       animal1.setAge(new Long(1));
       aa.add(animal1);
       }
       {
       Animal animal2 = new Animal();
       animal2.setName("Animal.2");
       animal2.setAge(new Long(2));
       aa.add(animal2);
       }
       return aa;
       }else{
       ArrayList<Human> bb = new ArrayList<Human>();
       {
       Human human1 = new Human();
       human1.setName("Human.1");
       human1.setAge(new Long(1));
       bb.add(human1);
       }
       {
       Human human2 = new Human();
       human2.setName("Human.2");
       human2.setAge(new Long(2));
       bb.add(human2);
       }
       return bb;
       }
       }
      
      
      }
      
      
      





        • 1. Re: Problem of polymorphism Web Service in EJB
          ybxiang.china

          ============below codes are for Client test ============
          ============below codes are for Client test ============
          ============below codes are for Client test ============

          
          /**
           * EJBWebServiceBean.java
           *
           * This file was auto-generated from WSDL
           * by the Apache Axis 1.4 Apr 22, 2006 (06:55:48 PDT) WSDL2Java emitter.
           */
          
          package com.ybxiang.ejbws;
          
          import javax.jws.WebMethod;
          import javax.jws.WebService;
          
          
          @WebService
          public interface EJBWebServiceBean extends java.rmi.Remote {
           @WebMethod
           public void passParameter1(java.lang.String arg0) throws java.rmi.RemoteException;
          
           @WebMethod
           public void passParameter2(com.ybxiang.ejbws.Animal arg0) throws java.rmi.RemoteException;
           @WebMethod
           public void passParameter3(com.ybxiang.ejbws.Human arg0) throws java.rmi.RemoteException;
           @WebMethod
           public void passParameter4(com.ybxiang.ejbws.Animal[] arg0) throws java.rmi.RemoteException;
           @WebMethod
           public com.ybxiang.ejbws.Animal[] returnSomething1(int arg0) throws java.rmi.RemoteException;
          }
          
          
          
          
          
          
          package com.ybxiang.ejbws;
          
          import java.net.URL;
          
          import javax.xml.namespace.QName;
          import javax.xml.ws.Service;
          
          
          
          public class TestMain {
           public static void main(String []args){
           try {
           URL location = new URL("http://127.0.0.1:8080/PolymorphismEJBWebService/EJBWebServiceBean?wsdl");
           QName serviceName = new QName("http://ejbws.ybxiang.com/","EJBWebServiceBeanService");
          
           Service service = Service.create(location, serviceName);
           //service.setHandlerResolver(new EmsHandlerResolver());
          
           QName port = new QName("http://ejbws.ybxiang.com/","EJBWebServiceBeanPort");
           EJBWebServiceBean ejbWebServiceBean = service.getPort(port, EJBWebServiceBean.class);
          
           Animal a1 = new Animal();
           a1.setName("Animal.1");
           a1.setAge(new Long(1));
           //
           Animal a2 = new Animal();
           a2.setName("Animal.2");
           a2.setAge(new Long(2));
           //
           Human h = new Human();
           h.setName("Human.1");
           h.setAge(new Long(1));
           h.setEmail("human1@gmail.com");
           System.out.println("===================testing===========================");
           System.out.println("test:ejbWebServiceBean.passParameter1(\"Test string\");");
           ejbWebServiceBean.passParameter1("Test string");
          
           System.out.println("test:ejbWebServiceBean.passParameter2(a1);");
           ejbWebServiceBean.passParameter2(a1);
          
           System.out.println("test:ejbWebServiceBean.passParameter2(h);");
           ejbWebServiceBean.passParameter2(h);
          
           System.out.println("test:ejbWebServiceBean.passParameter3(h);");
           ejbWebServiceBean.passParameter3(h);
           //
           Animal aa[] = new Animal[]{a1,a2};
           System.out.println("test:ejbWebServiceBean.passParameter4(aa);");
           ejbWebServiceBean.passParameter4(aa);
           //
           Human hh[] = new Human[]{h};
           System.out.println("test:ejbWebServiceBean.passParameter4(hh);");
           ejbWebServiceBean.passParameter4(hh);
           //
           System.out.println("---------------[animal]------------------");
           Animal[] returnedValue1 = ejbWebServiceBean.returnSomething1(1);
           System.out.println("------------returnedValue1:");
           for(Animal item:returnedValue1){
           System.out.println(" *** item:"+item);
           }
          
           System.out.println("---------------[human]------------------");
           Animal[] returnedValue2 = ejbWebServiceBean.returnSomething1(2);
           System.out.println("------------returnedValue2:");
           for(Animal item:returnedValue2){
           System.out.println(" *** item:"+item);
           }
          
          
           } catch (Exception e) {
           e.printStackTrace();
           }
           }
          }
          
          
          


          • 2. Re: Problem of polymorphism Web Service in EJB
            ybxiang.china

            ============== result from JBoss AS==============
            ============== result from JBoss AS==============
            ============== result from JBoss AS==============

            
            
            18:05:01,199 DEBUG [ManagerBase] Start expire sessions StandardManager at 1257329101199 sessioncount
             0
            18:05:01,199 DEBUG [ManagerBase] End expire sessions StandardManager processingTime 0 expired sessio
            ns: 0
            18:05:07,230 FINE [tcp] RMI Scheduler(0): close connection
            18:05:07,230 FINE [tcp] RMI TCP Connection(9)-127.0.0.1: (port 1558) connection closed
            18:05:07,230 FINE [tcp] RMI TCP Connection(9)-127.0.0.1: close connection
            18:05:26,120 WARN [StatelessBeanContext] EJBTHREE-1337: do not get WebServiceContext property from
            stateless bean context, it should already have been injected
            18:05:26,120 INFO [STDOUT] ------------String is got:Test string
            18:05:26,120 WARN [StatelessBeanContext] EJBTHREE-1337: do not get WebServiceContext property from
            stateless bean context, it should already have been injected
            18:05:26,136 WARN [StatelessBeanContext] EJBTHREE-1337: do not get WebServiceContext property from
            stateless bean context, it should already have been injected
            18:05:26,136 INFO [STDOUT] ------------Animal is got:Animal-->name:Animal.1,age:1
            18:05:26,136 WARN [StatelessBeanContext] EJBTHREE-1337: do not get WebServiceContext property from
            stateless bean context, it should already have been injected
            18:05:26,136 WARN [StatelessBeanContext] EJBTHREE-1337: do not get WebServiceContext property from
            stateless bean context, it should already have been injected
            18:05:26,136 INFO [STDOUT] ------------Animal is got:Animal-->name:Human.1,age:1
            18:05:26,136 WARN [StatelessBeanContext] EJBTHREE-1337: do not get WebServiceContext property from
            stateless bean context, it should already have been injected
            18:05:26,151 WARN [StatelessBeanContext] EJBTHREE-1337: do not get WebServiceContext property from
            stateless bean context, it should already have been injected
            18:05:26,151 INFO [STDOUT] ------------Human is got, name:Human.1, email:human1@gmail.com
            18:05:26,151 WARN [StatelessBeanContext] EJBTHREE-1337: do not get WebServiceContext property from
            stateless bean context, it should already have been injected
            18:05:26,151 WARN [StatelessBeanContext] EJBTHREE-1337: do not get WebServiceContext property from
            stateless bean context, it should already have been injected
            18:05:26,151 INFO [STDOUT] ------------ArrayList<Animal> is got:
            18:05:26,151 INFO [STDOUT] One Animal is got:Animal-->name:Animal.1,age:1
            18:05:26,151 WARN [StatelessBeanContext] EJBTHREE-1337: do not get WebServiceContext property from
            stateless bean context, it should already have been injected
            18:05:26,151 WARN [StatelessBeanContext] EJBTHREE-1337: do not get WebServiceContext property from
            stateless bean context, it should already have been injected
            18:05:26,151 INFO [STDOUT] ------------ArrayList<Animal> is got:
            18:05:26,151 INFO [STDOUT] One Animal is got:Animal-->name:Human.1,age:1
            18:05:26,151 WARN [StatelessBeanContext] EJBTHREE-1337: do not get WebServiceContext property from
            stateless bean context, it should already have been injected
            18:05:26,167 WARN [StatelessBeanContext] EJBTHREE-1337: do not get WebServiceContext property from
            stateless bean context, it should already have been injected
            1
            
            
            




            ============== result from client ==============
            Nov 4, 2009 6:05:25 PM com.sun.xml.internal.ws.model.RuntimeModeler getResponseWrapperClass
            INFO: Dynamically creating response wrapper bean Class com.ybxiang.ejbws.jaxws.ReturnSomething1Respo
            nse
            ===================testing===========================
            test:ejbWebServiceBean.passParameter1("Test string");
            test:ejbWebServiceBean.passParameter2(a1);
            test:ejbWebServiceBean.passParameter2(h);
            test:ejbWebServiceBean.passParameter3(h);
            test:ejbWebServiceBean.passParameter4(aa);
            test:ejbWebServiceBean.passParameter4(hh);
            ---------------[animal]------------------
            ------------returnedValue1:
             *** item:Animal-->name:Animal.1,age:1
             *** item:Animal-->name:Animal.2,age:2
            ---------------[human]------------------
            ------------returnedValue2:
             *** item:Animal-->name:Human.1,age:1
             *** item:Animal-->name:Human.2,age:2
            
            



            • 3. Re: Problem of polymorphism Web Service in EJB
              ybxiang.china

              I hope JBoss AS print something like:


              18:05:26,136 WARN [StatelessBeanContext] EJBTHREE-1337: do not get WebServiceContext property from
              stateless bean context, it should already have been injected
              18:05:26,136 INFO [STDOUT] ------------Animal is got:Animal-->name:Human.1,age:1

              ~~~~~~~~~~~~~~I hope here print "Human is got:Human-->name:Human.1,age:1"

              • 4. Re: Problem of polymorphism Web Service in EJB
                ybxiang.china

                Any tips will be appreciated!!!

                • 5. Re: Problem of polymorphism Web Service in EJB
                  ybxiang.china

                  I googled 2 days, and read "ejb in action", "enterprise java bean3". but got nothing useful.