Renier
Renier

Reputation: 1830

How to Return an Class with an Array in Java webservices Consumed by C#

I am having trouble creating a java webservice that returns an Array inside a class. I created a java websrevice with a class and inside the class I created a new Array that will return another Class. But when Importing the WSDL in a C# project I cant access the Array inside the class.

My java web service example :

My industry class :

public class Industry {

        public int industryID;
        public String industryName;
        public Product[ ] products;


}

The Idea is to return the industry with all the products of the industry.

The Product Class :

public class Product {

    public int productID;
    public String productName;



}

My webservice that populates the industry and the Products for the Industry. Please nte I know I am suposed to create get and set methods to set the values.. I only created a small examle of my problem.

My Webservice class :

public class IndustryService {
    /**
     * @param industryID
     * @return industry object
     */
    public Industry getIndustryData(int industryID){

        Product product1 = new Product();
        product1.productID = 712;
        product1.productName = "Sensor Light";

        Product product2 = new Product();
        product2.productID = 1774;
        product2.productName = "Light Beamer";

        Product [] products = new Product[] { product1, product2 };

        Industry industry = new Industry();
        industry.industryID = 2311;
        industry.industryName = "Test";
        industry.products = products;

        return industry;
    }
}

Here's the WSDL that gets generated in java :

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions targetNamespace="http://server.com" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://server.com" xmlns:intf="http://server.com" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<!--WSDL created by Apache Axis version: 1.4
Built on Apr 22, 2006 (06:55:48 PDT)-->
 <wsdl:types>
  <schema elementFormDefault="qualified" targetNamespace="http://server.com" xmlns="http://www.w3.org/2001/XMLSchema">
   <element name="getIndustryData">
    <complexType>
     <sequence>
      <element name="industryID" type="xsd:int"/>
     </sequence>
    </complexType>
   </element>
   <element name="getIndustryDataResponse">
    <complexType>
     <sequence>
      <element name="getIndustryDataReturn" type="impl:Industry"/>
     </sequence>
    </complexType>
   </element>
   <complexType name="Product">
    <sequence>
     <element name="productID" type="xsd:int"/>
     <element name="productName" nillable="true" type="xsd:string"/>
    </sequence>
   </complexType>
   <complexType name="ArrayOfProduct">
    <sequence>
     <element maxOccurs="unbounded" minOccurs="0" name="item" type="impl:Product"/>
    </sequence>
   </complexType>
   <complexType name="Industry">
    <sequence>
     <element name="industryID" type="xsd:int"/>
     <element name="industryName" nillable="true" type="xsd:string"/>
     <element name="products" nillable="true" type="impl:ArrayOfProduct"/>
    </sequence>
   </complexType>
  </schema>
 </wsdl:types>

   <wsdl:message name="getIndustryDataResponse">

      <wsdl:part element="impl:getIndustryDataResponse" name="parameters">

      </wsdl:part>

   </wsdl:message>

   <wsdl:message name="getIndustryDataRequest">

      <wsdl:part element="impl:getIndustryData" name="parameters">

      </wsdl:part>

   </wsdl:message>

   <wsdl:portType name="IndustryService">

      <wsdl:operation name="getIndustryData">

         <wsdl:input message="impl:getIndustryDataRequest" name="getIndustryDataRequest">

       </wsdl:input>

         <wsdl:output message="impl:getIndustryDataResponse" name="getIndustryDataResponse">

       </wsdl:output>

      </wsdl:operation>

   </wsdl:portType>

   <wsdl:binding name="IndustryServiceSoapBinding" type="impl:IndustryService">

      <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>

      <wsdl:operation name="getIndustryData">

         <wsdlsoap:operation soapAction=""/>

         <wsdl:input name="getIndustryDataRequest">

            <wsdlsoap:body use="literal"/>

         </wsdl:input>

         <wsdl:output name="getIndustryDataResponse">

            <wsdlsoap:body use="literal"/>

         </wsdl:output>

      </wsdl:operation>

   </wsdl:binding>

   <wsdl:service name="IndustryServiceService">

      <wsdl:port binding="impl:IndustryServiceSoapBinding" name="IndustryService">

         <wsdlsoap:address location="http://localhost:8080//IIIII/services/IndustryService"/>

      </wsdl:port>

   </wsdl:service>

</wsdl:definitions>

Now when consuming it using c# I expect to get a Industry Containing 2 products but C# shows that theres 0 inside of product array...

C# example created a normal form and imported the java WSDL as service reference:

enter image description here

private void Form1_Load(object sender, EventArgs e)
        {
            ServiceReference1.IndustryServiceClient client = new WindowsFormsApplication4.ServiceReference1.IndustryServiceClient();

            ServiceReference1.Industry m = client.getIndustryData(2);

            string a = "test";

        } 

When I debug windows form I get the following :

enter image description here

Notice the Product Array count is 0?

Why is this zero and What am I doing wrong?

Is the problem in the java side or c# side?

I am using eclipse to create the java webservice and Visual studio to import wsdl.

In soap UI I also imported the WSDL just to test the webservice to see the request and response and It looks correct :

Request :

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://server.com">
   <soapenv:Header/>
   <soapenv:Body>
      <ser:getIndustryData>
         <ser:industryID>2</ser:industryID>
      </ser:getIndustryData>
   </soapenv:Body>
</soapenv:Envelope>

Response:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soapenv:Body>
      <getIndustryDataResponse xmlns="http://server.com">
         <getIndustryDataReturn>
            <industryID>2311</industryID>
            <industryName>Test</industryName>
            <products>
               <productID>712</productID>
               <productName>Sensor Light</productName>
            </products>
            <products>
               <productID>1774</productID>
               <productName>Light Beamer</productName>
            </products>
         </getIndustryDataReturn>
      </getIndustryDataResponse>
   </soapenv:Body>
</soapenv:Envelope>

Code in the reference.cs file I edited :

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "3.0.0.0")]
[System.Runtime.Serialization.CollectionDataContractAttribute(Name="products", Namespace="http://server.com", ItemName="item")]
[System.SerializableAttribute()]
public class ArrayOfProduct : System.Collections.Generic.List<WindowsFormsApplication4.ServiceReference1.Product> {
}

In the Reference.cs file I changed the Name and ItemName tag to Products and now it is working.

[System.Runtime.Serialization.CollectionDataContractAttribute(Name = "products", Namespace = "http://server.com", ItemName = "products")]

Upvotes: 1

Views: 1712

Answers (1)

Oguz Ozgul
Oguz Ozgul

Reputation: 7187

Visual Studio creates the stub and expects the elements of products with the tag name "ArrayOfProduct" but the server sends them with the following tag

<products>

See the reference.cs file under the service reference (you need to check the Show All Files for the project).

It is not a good practice, but to resolve the issue, you can manipulate the reference.cs file manually to change the CollectionDataContractAttribute to "products" on top of the definition of public class ArrayOfProduct

Upvotes: 1

Related Questions