如何在 C# 使用的 Java Web 服务中 Return 一个带有数组的 Class

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

我在创建 java 网络服务时遇到问题,该网络服务 return 是 class 中的一个数组。我创建了一个带有 class 的 java websrevice,并在 class 中创建了一个新数组,它将 return 另一个 Class。但是在 C# 项目中导入 WSDL 时,我无法访问 class.

中的数组

我的 java 网络服务示例:

我的行业class:

public class Industry {

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


}

Idea 是return 行业的所有产品。

产品Class:

public class Product {

    public int productID;
    public String productName;



}

我的网络服务填充了行业和行业产品。请注意,我知道我应该创建 get 和 set 方法来设置值。我只为我的问题创建了一个小例子。

我的网络服务 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;
    }
}

这是在 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 作为服务引用:

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 表单时,我得到以下信息:

注意到产品数组计数为 0 了吗?

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

问题出在java端还是c#端?

我正在使用 eclipse 创建 java 网络服务并 Visual studio 导入 wsdl。

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

要求:

<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")]

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

<products>

查看服务引用下的reference.cs文件(需要勾选项目的Show All Files)。

这不是一个好的做法,但要解决此问题,您可以手动操作 reference.cs 文件,在 public 的定义之上将 CollectionDataContractAttribute 更改为 "products" classArrayOfProduct