0

Previoysly I wrote a C# client to consume a conventional web service, which accepts an object of an OrderInfo class as a parameter. OrderInfo class has CustomerID and SpecialInstructions fields and a List. Product has ProductID, Quantity and optional PriceOverride.

It was very simple to create those and pass to WS in C# as in the below sample:

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. How can I add products to the order in Java?

Thank you!

ajeh
  • 2,652
  • 2
  • 34
  • 65

3 Answers3

0

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. It won't be as easy as it is in .NET, but it will end up being object-oriented.

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

See this answer for some details on using javax.xml.soap.

Community
  • 1
  • 1
rrhartjr
  • 2,112
  • 2
  • 15
  • 21
  • I corrected the question to incidate that the web service is conventional for clarity. Even though it is non-WCF, I do have WSDL and it's been imported into Java project. That provided me with access to the classes and methods of the web service. That takes care of generic things you have mentioned. My question is rather specific as regards to how can I add items to a list member of .NET class which web service method expects as a parameter. – ajeh Aug 28 '13 at 16:02
  • After some time I found a way to insert items into the OrderInfo's list of ProductInfo: – ajeh Aug 28 '13 at 19:27
  • If it would be useful to someone else, please add and accept your own answer. – rrhartjr Aug 28 '13 at 19:49
0

After some time I found a way to insert items into the OrderInfo's list of ProductInfo. Now after 8 hours I can finally post the solution (as I have <10 reputation, site would not allow me earlier).

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;
ajeh
  • 2,652
  • 2
  • 34
  • 65
0

Another related question just arose when the web service was changed on the .NET side by adding a method returning DataTable. WSDL included the following bit which is causing grief for NetBeans now:

<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. NetBeans is throwing the following error when refreshing web reference:

Web service client cannot be created by JAXWS:wsimport utility. Reason: property 'Any' is already defined. Use &lt:jaxb:property> to resolve this conflict.

There might be a problem during java artifacts creation: for example a name conflict in generated classes. To detect the problem see also the error message in output window. You may be able to fix the problem in WSDL customization dialogue. (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)

Can we manually edit out offending methods out of WSDL and put WSDL file into NetBeans project directory? Or should we just delete web reference and re-create, providing path to downloaded WSDL file?

ajeh
  • 2,652
  • 2
  • 34
  • 65
  • Yes, deleting the new method from a local copy of WSDL and refreshing web reference fixed the issue. – ajeh Aug 30 '13 at 15:56