gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 07:49:28 25 4
gpt4 key购买 nike

我在创建一个在类中返回数组的 java webservice 时遇到问题。我使用一个类创建了一个 java websrevice,并在该类中创建了一个新的数组,该数组将返回另一个类。但是,当在 C# 项目中导入 WSDL 时,我无法访问类内的数组。

我的 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 方法来设置值。我只创建了我的问题的一个小例子。

我的网络服务类:

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只是为了测试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 创建 stub 并需要具有标签名称“ArrayOfProduct”的产品元素,但服务器使用以下标签发送它们

<products>

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

这不是一个好的做法,但要解决该问题,您可以手动操作reference.cs文件,将CollectionDataContractAttribute更改为公共(public)类ArrayOfProduct定义之上的“products”

关于java - 如何在 C# 使用的 Java Web 服务中返回带有数组的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33472150/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com