5 Replies Latest reply on Jul 28, 2010 12:51 PM by alex soto

    Abstract class as input Parameter in WebService

    Vincenzo Esposito Newbie

      Hi, I've a problem to use abstract class in a Web Service like WebMethod input parameter. I'm using JBOSS-4.2.2.GA and JBOSSWS-NATIVE-3.0.3. Specifically, I've read a lot of documents and example about how to use @XmlSeeAlso annotation and there are two different kinds explained:
      1) use it in WebService class reporting classes that extend Abstract class or
      2) use it in Abstract class.
      The first one doesn't generate stubs (using wsconsume) relative to real Classes, so it's not possible use them client-side, the second one instead generates them so client can create a class to use like parameter in WebMethod.
      Supponing that the second way it's the correct one, i generate stubs to use them client side and this is code about service, abstract class and real class:

      WakeRider.java

      @WebService(serviceName="WakeRider")
      @Stateless
      
      public class WakeRider {
      
       public String showItem(Item item) {
       try {
       if(item instanceof Tower)
       System.out.println("inside");
       return ((Tower)item).ItemName();
       } catch (Exception e) {
       e.printStackTrace();
       }
       return "";
       }
      
      }
      


      Item.java
      @XmlSeeAlso({Tower.class})
      public abstract class Item implements Serializable {
      
       public abstract String ItemName();
      
      }
      


      Tower.java
      @XmlType(name="Tower",propOrder={"itemName"})
      public class Tower extends Item {
      
       private String itemName;
      
       @Override
       public String ItemName() {
       // TODO Auto-generated method stub
       return itemName;
       }
       public String getItemName() {
       return itemName;
       }
       public void setItemName(String itemName) {
       this.itemName = itemName;
       }
      }
      


      Generated WSDL is this:
      <definitions name="WakeRider" targetNamespace="http://wakerider/">
      -
       <types>
      -
       <xs:schema targetNamespace="http://wakerider/" version="1.0">
      <xs:element name="showItem" type="tns:showItem"/>
      <xs:element name="showItemResponse" type="tns:showItemResponse"/>
      -
       <xs:complexType name="showItem">
      -
       <xs:sequence>
      <xs:element minOccurs="0" name="arg0" type="tns:item"/>
      </xs:sequence>
      </xs:complexType>
      -
       <xs:complexType abstract="true" name="item">
      <xs:sequence/>
      </xs:complexType>
      -
       <xs:complexType name="Tower">
      -
       <xs:complexContent>
      -
       <xs:extension base="tns:item">
      -
       <xs:sequence>
      <xs:element minOccurs="0" name="itemName" type="xs:string"/>
      </xs:sequence>
      </xs:extension>
      </xs:complexContent>
      </xs:complexType>
      -
       <xs:complexType name="showItemResponse">
      -
       <xs:sequence>
      <xs:element minOccurs="0" name="return" type="xs:string"/>
      </xs:sequence>
      </xs:complexType>
      </xs:schema>
      </types>
      -
       <message name="WakeRider_showItemResponse">
      <part element="tns:showItemResponse" name="showItemResponse"/>
      </message>
      -
       <message name="WakeRider_showItem">
      <part element="tns:showItem" name="showItem"/>
      </message>
      -
       <portType name="WakeRider">
      -
       <operation name="showItem" parameterOrder="showItem">
      <input message="tns:WakeRider_showItem"/>
      <output message="tns:WakeRider_showItemResponse"/>
      </operation>
      </portType>
      -
       <binding name="WakeRiderBinding" type="tns:WakeRider">
      <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
      -
       <operation name="showItem">
      <soap:operation soapAction=""/>
      -
       <input>
      <soap:body use="literal"/>
      </input>
      -
       <output>
      <soap:body use="literal"/>
      </output>
      </operation>
      </binding>
      -
       <service name="WakeRider">
      -
       <port binding="tns:WakeRiderBinding" name="WakeRiderPort">
      <soap:address location="http://localhost:8080/AbstractTest/WakeRider"/>
      </port>
      </service>
      </definitions>
      


      Using this WSDL I generate stubs to use client side. Client is defined below:

      Client.java
      public class Client {
      
       public static void main(String[] args) {
       WakeRider_Service srv = new WakeRider_Service();
       WakeRider wr_srv = srv.getWakeRiderPort();
      
       Tower item=new Tower();
       item.setItemName("tower");
       System.out.println(wr_srv.showItem(item));
       }
      
      }
      


      When I start this client i've this Exception server side:
      ERROR [SOAPFaultHelperJAXWS] SOAP request exception
      javax.xml.ws.WebServiceException: javax.xml.bind.UnmarshalException: Unable to create an instance of wakerider.inventory.Item
       - with linked exception:
      [java.lang.InstantiationException]
       at org.jboss.ws.core.jaxws.JAXBDeserializer.handleUnmarshallException(JAXBDeserializer.java:110)
       at org.jboss.ws.core.jaxws.JAXBDeserializer.deserialize(JAXBDeserializer.java:78)
       at org.jboss.ws.core.binding.DeserializerSupport.deserialize(DeserializerSupport.java:58)
      .....
      


      Thanks for help.

        • 1. Re: Abstract class as input Parameter in WebService
          Santhosh Nair Newbie

          I used SOAP UI (soapui.org) to send the web service requests and following code worked for me

          
          <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://www.searchforce.net/oneplace-ws/services">
           <soapenv:Header/>
           <soapenv:Body>
           <ser:showItem>
           <!--Optional:-->
           <Item xsi:type="Tower" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
           <!--Optional:-->
           <itemName>Test items</itemName>
           </Item>
           </ser:showItem>
           </soapenv:Body>
          </soapenv:Envelope>
          


          This code worked with JBOSS 4.0.5 as well as JBOSS 4.2.3.
          I do receive this exception on first try. I think the problem was in setting xsi:type correctly.


          • 2. Re: Abstract class as input Parameter in WebService
            Sandeep Nair Newbie

            HI,
            I also have a similar problem with my webservice..
            how did u solve the issue..

            Thanks and Regards
            Sandeep

            • 3. Re: Abstract class as input Parameter in WebService
              Vincenzo Esposito Newbie

              Hi,
              I've not found a solution to problem, so I cannot suggest you the solution. I was waiting for an answer but maybe this issue had not been solved.

              • 4. Re: Abstract class as input Parameter in WebService
                alex soto Newbie

                I am now facing this same problem with JBoss 5.1.0.GA.

                Can anybody help? 

                 

                -Thanks

                • 5. Re: Abstract class as input Parameter in WebService
                  alex soto Newbie

                  In case it helps somebody else, here is how I managed to get the JBoss web service client to work.

                   

                  With Java 6, need to run the client with the JVM option:

                   

                  -Djava.endorsed.dirs=${JBOSS_HOME}/lib/endorsed
                  

                   

                  The maven dependencies I used are:

                   

                  <dependency>
                    <groupId>org.jboss.jbossas</groupId>
                    <artifactId>jboss-as-client</artifactId>
                    <version>5.1.0.GA</version>
                    <type>pom</type>
                    <scope>compile</scope>
                    <exclusions>
                      <exclusion>
                        <artifactId>jaxb-api</artifactId>
                        <groupId>sun-jaxb</groupId>
                      </exclusion>
                    </exclusions>
                  </dependency>
                  <dependency>
                    <groupId>org.jboss</groupId>
                    <artifactId>jboss-common-core</artifactId>
                    <version>2.2.14.GA</version>
                    <scope>compile</scope>
                  </dependency>
                  <dependency>
                    <groupId>org.jboss.ws</groupId>
                    <artifactId>jbossws-spi</artifactId>
                    <version>1.1.2.GA</version>
                  </dependency>
                  <dependency>
                    <groupId>org.jboss.ws.native</groupId>
                    <artifactId>jbossws-native-jaxws</artifactId>
                    <version>3.1.2.GA</version>
                    <exclusions>
                      <exclusion>
                        <artifactId>stax-api</artifactId>  
                        <groupId>javax.xml.stream</groupId>
                     </exclusion>
                    </exclusions>
                  </dependency>
                  <dependency>
                    <groupId>org.jboss.client</groupId>
                    <artifactId>jboss-client</artifactId>
                    <version>5.1.0.CR1</version>
                  </dependency>
                  <dependency>
                    <groupId>javassist</groupId>
                    <artifactId>javassist</artifactId>
                    <version>3.9.0.GA</version>
                  </dependency>
                  <dependency>
                    <groupId>org.jboss.client</groupId>
                    <artifactId>jbossall-client</artifactId>
                    <version>5.0.0.GA</version>
                  </dependency>
                  <dependency>
                    <groupId>org.jboss</groupId>
                    <artifactId>jbossxb</artifactId>
                    <version>2.0.1.GA</version>
                    <scope>compile</scope>
                  </dependency>
                  <dependency>
                    <groupId>org.jboss.ws</groupId>
                    <artifactId>jbossws-common</artifactId>
                    <version>1.1.0.SP1</version>
                  </dependency>
                  <dependency>
                    <groupId>org.jboss.ws.native</groupId>
                    <artifactId>jbossws-native-client</artifactId>
                    <version>3.1.2.GA</version>
                    <exclusions>
                         <exclusion>
                              <artifactId>stax-api</artifactId>
                              <groupId>stax</groupId>
                         </exclusion>
                    </exclusions>
                  </dependency>
                  <dependency>
                    <groupId>org.jboss.ws.native</groupId>
                    <artifactId>jbossws-native-core</artifactId>
                    <version>3.1.2.GA</version>
                    <exclusions>
                         <exclusion>
                              <artifactId>stax-api</artifactId>
                              <groupId>stax</groupId>
                         </exclusion>
                         <exclusion>
                              <artifactId>stax-api</artifactId>
                              <groupId>javax.xml.stream</groupId>
                         </exclusion>
                    </exclusions>
                  </dependency>
                  <dependency>
                    <groupId>org.jboss.ws.native</groupId>
                    <artifactId>jbossws-native-jaxrpc</artifactId>
                    <version>3.1.2.GA</version>
                  </dependency>
                  <dependency>
                    <groupId>org.jboss.ws.native</groupId>
                    <artifactId>jbossws-native-jaxws-ext</artifactId>
                    <version>3.1.2.GA</version> 
                    <scope>compile</scope>
                  </dependency>
                  <dependency>
                    <groupId>org.jboss.ws.native</groupId>
                    <artifactId>jbossws-native-saaj</artifactId>
                    <version>3.1.2.GA</version>
                  </dependency>
                  <dependency>
                    <groupId>com.sun.xml.fastinfoset</groupId>
                    <artifactId>FastInfoset</artifactId>
                    <version>1.2.2</version>
                  </dependency>
                  

                   

                   

                  These dependencies may include more than what is actually needed, but I spent a lot of time adding one by one until it worked, so I have no energy left to clean up redundant or superfluous dependencies

                   

                  Again, this worked for JBoss 5.1.0.GA and Java 6.