Create SOAP Request with Java
The Membrane SOA Model can create SOAP requests out of a WSDL document and a Java Map filled with the data for the request.01.package sample.wsdl;02. 03.import groovy.xml.MarkupBuilder;04.import java.io.StringWriter;05.import java.util.HashMap;06. 07.import com.predic8.wsdl.*;08.import com.predic8.wstool.creator.*;09. 10.public class CreateSOAPRequest {11. 12.public static void main(String[] args) {13. 14.WSDLParser parser = new WSDLParser();15. 16.Definitions wsdl = parser.parse("resources/article/article.wsdl");17. 18.StringWriter writer = new StringWriter();19. 20.HashMap<String, String> formParams = new HashMap<String, String>();21.formParams.put("xpath:/create/article/name", "foo");22.formParams.put("xpath:/create/article/description", "bar");23.formParams.put("xpath:/create/article/price/amount", "00.00");24.formParams.put("xpath:/create/article/price/currency", "USD");25.formParams.put("xpath:/create/article/id", "1");26. 27.SOARequestCreator creator = new SOARequestCreator();28.creator.setBuilder(new MarkupBuilder(writer));29.creator.setDefinitions(wsdl);30.creator.setFormParams(formParams);31.creator.setCreator(new RequestCreator());32. 33.//creator.createRequest(PortType name, Operation name, Binding name);34.creator.createRequest("ArticleServicePT", "create", "ArticleServicePTBinding");35. 36.System.out.println(writer);37.}38.}Listing 1: CreateSOAPRequest
The following table shows the input for the SOARequestCreator.
| Key | Value |
|---|---|
| xpath:/create/article/name | foo |
| xpath:/create/article/description | bar |
| xpath:/create/article/price/amount | 00.00 |
| xpath:/create/article/price/currency | USD |
| xpath:/create/article/id | 1 |
Using the table above you get the following soap request message.
01.<s11:Envelope xmlns:s11='http://schemas.xmlsoap.org/soap/envelope/'>02.<s11:Body>03.<ns1:create xmlns:ns1='http://predic8.com/wsdl/material/ArticleService/1/'>04.<article xmlns:ns1='http://predic8.com/material/1/'>05.<name xmlns:ns1='http://predic8.com/material/1/'>foo</name>06.<description xmlns:ns1='http://predic8.com/material/1/'>bar</description>07.<price xmlns:ns1='http://predic8.com/common/1/'>08.<amount xmlns:ns1='http://predic8.com/common/1/'>00.00</amount>09.<currency xmlns:ns1='http://predic8.com/common/1/'>USD</currency>10.</price>11.<id xmlns:ns1='http://predic8.com/material/1/'>1</id>12.</article>13.</ns1:create>14.</s11:Body>15.</s11:Envelope>Listing 2: Created soap request message
Unbounded Elements
To create a list of repeating elements in a SOAP request square brackets can be used.Listing 3 shows how to set the formParams from Listing 1 to create a request with more than one item.
01....02.HashMap<String, String> formParams = new HashMap<String, String>();03.formParams.put("xpath:/order/items/item[1]/name", "apple");04.formParams.put("xpath:/order/items/item[1]/quantity", "3");05.formParams.put("xpath:/order/items/item[1]/price", ".95");06.formParams.put("xpath:/order/items/item[2]/name", "orange");07.formParams.put("xpath:/order/items/item[2]/quantity", "5");08.formParams.put("xpath:/order/items/item[2]/price", "1.45");09....Listing 3: CreateSOAPRequest with unbounded number of elements
No comments:
Post a Comment