1

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")]
18
  • Look at the message itself to figure out problem. I.e. use Fiddler HTTP debugger to capture requests and look at them. Commented Nov 2, 2015 at 6:54
  • What is the value of "productse" in the response? Your Industry class does not have a field with this name and it seems like Elipse is adding it to the WSDL Commented Nov 2, 2015 at 7:38
  • @OguzOzgul Sorry about that I updated to the WSDL to the corect one.. productse was when I tetsed the code with a Get function. Please check the WSDL again. Commented Nov 2, 2015 at 8:00
  • 1
    Ok the problem is, Visual Studio creates the stub and expects the elements of products with the tag name "ArrayOfProduct" but the server sends them with the 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 Commented Nov 2, 2015 at 8:22
  • 1
    I might suggest a solution however; a workaround which sometimes we have to apply to our .net classes to achieve expected xml output when they are serialized. You can try to wrap the Product[ ] products field of the Industry class on server side in another class like ProductCollection, and can add a dummy public variable (we mostly use int dummy). I think in that case, the java web service and .net client (when service reference is updated) will be able communicate without client stub modifications. Worth trying I guess.. Commented Nov 3, 2015 at 8:11

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.