在Java中使用.NET webservice

时间:2013-08-28 15:41:18

标签: c# java web-services

Previoysly我编写了一个C#客户端来使用传统的Web服务,该服务接受OrderInfo类的对象作为参数。 OrderInfo类具有CustomerID和SpecialInstructions字段以及List。产品有ProductID,Quantity和可选的PriceOverride。

创建它们并在C#中传递给WS非常简单,如下例所示:

OrderEntryService s = new OrderEntryService();

OrderInfo o = new OrderInfo();

o.CustomerId = 1;
o.Items.Add(new ProductInfo(2, 4));
o.Items.Add(new ProductInfo(1, 2, 3.95m));

checkBox1.Checked = s.CreateOrder(o);

现在在Java中我只能访问get和set方法,这有点令人困惑,因为我只能通过调用o.getItems()来获取ArrayOfProductInfo,而不是能够将ProductInfo直接添加到OrderInfo中的列表中。如何在Java中将产品添加到订单?

谢谢!

3 个答案:

答案 0 :(得分:0)

假设这些是WCF服务,您应该能够获得可用作JAX-WSApache CXF之类的输入的WSDL。它不像在.NET中那么容易,但最终会面向对象。

如果您的用例非常简单,您可以使用javax.xml.soap甚至JDOM(如果您特别勇敢)来推送自己的SOAP消息。

有关使用javax.xml.soap的详细信息,请参阅this answer

答案 1 :(得分:0)

一段时间后,我找到了将项目插入OrderInfo的ProductInfo列表的方法。 现在8小时后我终于可以发布解决方案了(因为我有10点声望,网站不允许我提前)。

OrderInfo oi = new OrderInfo();
oi.setCustomerId(1);
oi.setSpecialInstructions("Leave on porch");

ArrayOfProductInfo ap = new ArrayOfProductInfo(); // this is web service's class's list
List<ProductInfo> lp = ap.getProductInfo(); // here we obtain a generic list reference from the above

ProductInfo pinf = new ProductInfo();

pinf.productID = 2;
pinf.quantity = 14;
pinf.currPrice = new BigDecimal("3.95");

lp.add(pinf);

pinf = new VEProductInfo();
pinf.productID = 4;
pinf.quantity = 6;
pinf.currPrice = new BigDecimal("0");

lp.add(pinf); // second product

oi.setItems(ap); // this adds product list to the order object!

WebService s = new WebService();
WebServiceSoap soapport = s.getWebServiceSoap();
soapport.createOrder(oi); // voila, passing order to the web service method.

这需要以下导入:

import java.math.BigDecimal;
import java.util.List;

答案 2 :(得分:0)

通过添加返回DataTable的方法在.NET端更改Web服务时,出现了另一个相关问题。 WSDL包括以下内容,它现在引起了NetBeans的悲痛:

<s:element name="GetProductsResponse">
  <s:complexType>
    <s:sequence>
      <s:element minOccurs="0" maxOccurs="1" name="GetProductsResult">
        <s:complexType>
          <s:sequence>
            <s:any minOccurs="0" maxOccurs="unbounded" namespace="http://www.w3.org/2001/XMLSchema" processContents="lax"/>
            <s:any minOccurs="1" namespace="urn:schemas-microsoft-com:xml-diffgram-v1" processContents="lax"/>
          </s:sequence>
        </s:complexType>
      </s:element>
    </s:sequence>
  </s:complexType>
</s:element>

我们意识到特定的.NET类不能在Java中轻松使用,甚至不会使用返回该类的方法,但我们仍然需要继续使用整个Web服务。刷新Web引用时,NetBeans会抛出以下错误:

  

JAXWS无法创建Web服务客户端:wsimport实用程序。   原因:已定义属性“任何”。使用&amp; lt:jaxb:property&gt;至   解决这个冲突。

     

在创建java工件期间可能存在问题:例如a   生成的类中的名称冲突。要检测问题,请参阅   输出窗口中的错误消息。您可能能够解决问题   WSDL定制对话。 (编辑Web服务属性部分)或   通过使用JAXB手动编辑本地wsdl或模式文件   自定义(本地wsdl和模式文件位于   xml-resources目录)

我们可以手动编辑WSDL中的违规方法并将WSDL文件放入NetBeans项目目录吗? 或者我们应该删除Web引用并重新创建,提供下载的WSDL文件的路径?