如何在Java Web服务中返回带有数组的类C#

时间:2015-11-02 06:50:17

标签: java c# eclipse web-services wsdl

我在创建一个在类中返回Array的java webservice时遇到了麻烦。我创建了一个带有类的java websrevice,在类中我创建了一个新的Array,它将返回另一个Class。但是当在C#项目中导入WSDL时,我无法访问类中的Array。

我的java web服务示例:

我的行业类:

public class Industry {

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


}

理念是让行业回归行业的所有产品。

产品类别:

public class Product {

    public int productID;
    public String productName;



}

我的网络服务填充了行业和行业产品。请知道我知道我可以创建get和set方法来设置值。我只创建了一个关于我的问题的小问题。

我的Web服务课程:

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;
    }
}

这是在java中生成的WSDL:

<?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>

现在使用c#消费它时,我希望得到一个包含2个产品的行业,但是C#显示产品阵列中有0个......

C#示例创建了一个普通表单并将java WSDL作为服务引用导入:

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";

        } 

当我调试Windows窗体时,我得到以下内容:

enter image description here

请注意,产品数组计数为0?

为什么这是零,我做错了什么?

是java方面还是c#方面的问题?

我使用eclipse创建java webservice和Visual studio来导入wsdl。

在soap UI中我还导入了WSDL,只是为了测试webservice以查看请求和响应,看起来是正确的:

请求:

<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>

响应:

<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>

我编辑的reference.cs文件中的代码:

[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> {
}

在Reference.cs文件中,我将Name和ItemName标记更改为Products,现在它正在运行。

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

1 个答案:

答案 0 :(得分:1)

Visual Studio创建存根,并期望产品的元素具有标记名称&#34; ArrayOfProduct&#34;但是服务器使用以下标记发送它们

<products>

请参阅服务引用下的reference.cs文件(您需要选中显示项目的所有文件)。

这不是一个好习惯,但要解决此问题,您可以手动操作reference.cs文件,将CollectionDataContractAttribute更改为&#34; products&#34;在公共类ArrayOfProduct

的定义之上