简体   繁体   English

使用Java消费.NET Web服务

[英]Consuming .NET webservice in Java

Previoysly I wrote a C# client to consume a conventional web service, which accepts an object of an OrderInfo class as a parameter. 以前,我编写了一个C#客户端来使用常规的Web服务,该服务接受OrderInfo类的对象作为参数。 OrderInfo class has CustomerID and SpecialInstructions fields and a List. OrderInfo类具有CustomerID和SpecialInstructions字段以及一个List。 Product has ProductID, Quantity and optional PriceOverride. 产品具有ProductID,数量和可选的PriceOverride。

It was very simple to create those and pass to WS in C# as in the below sample: 创建这些代码并通过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);

Now in Java I only have access to the get and set methods and it is a bit confusing, as I can only obtain an ArrayOfProductInfo by calling o.getItems() instead of being able to add ProductInfo directly to a list in OrderInfo. 现在在Java中,我只能访问get和set方法,这有点令人困惑,因为我只能通过调用o.getItems()获得ArrayOfProductInfo,而不能直接将ProductInfo添加到OrderInfo的列表中。 How can I add products to the order in Java? 如何在Java中将产品添加到订单中?

Thank you! 谢谢!

Assuming these are WCF services, you should be able to get the WSDL which can be used as an input to things like JAX-WS or Apache CXF . 假设这些是WCF服务,您应该能够获得WSDL,可以将其用作JAX-WSApache CXF之类的输入。 It won't be as easy as it is in .NET, but it will end up being object-oriented. 它不会像.NET那样简单,但是最终将是面向对象的。

If your use case is pretty simple, you may be able to roll your own SOAP messages using javax.xml.soap or even JDOM (if you are particularly brave). 如果您的用例非常简单,则可以使用javax.xml.soap甚至JDOM(如果您特别勇敢)来滚动自己的SOAP消息。

See this answer for some details on using javax.xml.soap. 有关使用javax.xml.soap的一些详细信息,请参见此答案

After some time I found a way to insert items into the OrderInfo's list of ProductInfo. 一段时间后,我找到了一种将商品插入OrderInfo的ProductInfo列表中的方法。 Now after 8 hours I can finally post the solution (as I have <10 reputation, site would not allow me earlier). 现在,在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.

This requires below imports: 这需要以下进口:

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

Another related question just arose when the web service was changed on the .NET side by adding a method returning DataTable. 当通过添加返回DataTable的方法在.NET端更改Web服务时,又出现了另一个相关问题。 WSDL included the following bit which is causing grief for NetBeans now: 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>

We realize that specific .NET classes cannot be easily consumed in Java and not going to even use the method returning that class, but we still need to continue consuming the web service as a whole. 我们意识到,特定的.NET类无法在Java中轻松使用,甚至不使用返回该类的方法,但是我们仍然需要继续使用整个Web服务。 NetBeans is throwing the following error when refreshing web reference: 刷新Web参考时,NetBeans引发以下错误:

Web service client cannot be created by JAXWS:wsimport utility. Web服务客户端不能通过JAXWS:wsimport实用程序创建。 Reason: property 'Any' is already defined. 原因:属性“ Any”已经定义。 Use &lt:jaxb:property> to resolve this conflict. 使用&lt:jaxb:property>解决此冲突。

There might be a problem during java artifacts creation: for example a name conflict in generated classes. 创建Java构件时可能会出现问题:例如,生成的类中的名称冲突。 To detect the problem see also the error message in output window. 要检测问题,另请参阅输出窗口中的错误消息。 You may be able to fix the problem in WSDL customization dialogue. 您也许可以在WSDL定制对话框中解决该问题。 (Edit web service attributes section) or by manual editing of the local wsdl or schema files, using JAXB customization (local wsdl and schema files are located in xml-resources directory) (编辑Web服务属性部分)或使用JAXB定制手动编辑本地wsdl或模式文件(本地wsdl和模式文件位于xml-resources目录中)

Can we manually edit out offending methods out of WSDL and put WSDL file into NetBeans project directory? 我们是否可以从WSDL中手动编辑违规方法并将WSDL文件放入NetBeans项目目录? Or should we just delete web reference and re-create, providing path to downloaded WSDL file? 还是我们应该删除Web参考并重新创建,以提供下载的WSDL文件的路径?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM