Al Grant
Al Grant

Reputation: 2354

Creating SOAP message with child node in Java

I want to send data to a SOAP service in Iava.

My IDE - Intellij Ultimate has auto generated the two main classes Sale and Item, and also ObjectFactory, SetSaleRequest, SetSaleResponse. I have been able to create a Sale instance but can not see how to add the child - items.

XML

    <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:typ="urn:some.com.au/schema/common/types">
   <soap:Header/>
   <soap:Body>
      <typ:setSalesRequest>
         <!--Optional:-->
         <typ:Sale>
            <!--Optional:-->
            <typ:Table>?</typ:Table>
            <!--Optional:-->
            <typ:SalesNo>?</typ:SalesNo>
            <typ:EnteredDateTime>2017-12-17T11:02:00.000+12:00</typ:EnteredateTime>
            <typ:SaleDateTime>?</typ:SaleDateTime>
            <!--Optional:-->
            <typ:Address>?</typ:Address>
            <typ:Summary>?</typ:Summary>
         </typ:Sale>
         <!--Zero or more repetitions:-->
         <typ:Item>
            <!--Optional:-->
            <typ:ItemCode>?</typ:ItemCode>
            <!--Optional:-->
            <typ:ItemDesc>?</typ:ItemDesc>
            <!--Optional:-->
            <typ:ItemCost>?</typ:IetmCost>
         </typ:Item>
      </typ:setSalesRequest>
   </soap:Body>
</soap:Envelope>

Code so far:

    // SALE
    Sale sale = new Sale();
    sale.setTable("East");
    ...
    sale.setSalesNo("INV001");

    // ITEM
    Item item = new Item();
    item.setItemCode("ABC123");
    ...
    item.setItemCost("$12.00");

    SetSaleRequest request = new SetSaleRequest();
    SetSaleResponse response = new SetSaleResponse();
    request.setSale(sale);
    SaleService saleService = new SaleService();
    ISaleService isaleService = saleService.getWSHttpBindingIISRService();
    isaleService.setSale(request);

However there is despite Item being a child of setSalesRequest in the XML there is no method exposed to add a Item.

I think I have to use ObjectFactory (which is another class that was auto generated from the WDSL).

How do I add a item to this request?

Upvotes: 0

Views: 359

Answers (1)

Mick Mnemonic
Mick Mnemonic

Reputation: 7956

Classes that are generated by JAXB handle lists of elements so that they provide a getter for accessing the list, istead of direct addXYZ() methods. In your case, the syntax for adding items to SetSaleRequest then becomes (a bit counter-intuitively):

request.getItems().add(item);

Upvotes: 2

Related Questions