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.
package sample.wsdl; import groovy.xml.MarkupBuilder; import java.io.StringWriter; import java.util.HashMap; import com.predic8.wsdl.*; import com.predic8.wstool.creator.*; public class CreateSOAPRequest { public static void main(String[] args) { WSDLParser parser = new WSDLParser(); Definitions wsdl = parser.parse("resources/article/article.wsdl"); StringWriter writer = new StringWriter(); HashMap<String, String> formParams = new HashMap<String, String>(); formParams.put("xpath:/create/article/name", "foo"); formParams.put("xpath:/create/article/description", "bar"); formParams.put("xpath:/create/article/price/amount", "00.00"); formParams.put("xpath:/create/article/price/currency", "USD"); formParams.put("xpath:/create/article/id", "1"); //SOARequestCreator constructor: SOARequestCreator(Definitions, Creator, MarkupBuilder) SOARequestCreator creator = new SOARequestCreator(wsdl, new RequestCreator(), new MarkupBuilder(writer)); creator.setFormParams(formParams); //creator.createRequest(PortType name, Operation name, Binding name); creator.createRequest("ArticleServicePT", "create", "ArticleServicePTBinding"); System.out.println(writer); } }
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.
<s11:Envelope xmlns:s11='http://schemas.xmlsoap.org/soap/envelope/'> <s11:Body> <ns1:create xmlns:ns1='http://predic8.com/wsdl/material/ArticleService/1/'> <article xmlns:ns1='http://predic8.com/material/1/'> <name xmlns:ns1='http://predic8.com/material/1/'>foo</name> <description xmlns:ns1='http://predic8.com/material/1/'>bar</description> <price xmlns:ns1='http://predic8.com/common/1/'> <amount xmlns:ns1='http://predic8.com/common/1/'>00.00</amount> <currency xmlns:ns1='http://predic8.com/common/1/'>USD</currency> </price> <id xmlns:ns1='http://predic8.com/material/1/'>1</id> </article> </ns1:create> </s11:Body> </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.
... HashMap<String, String> formParams = new HashMap<String, String>(); formParams.put("xpath:/order/items/item[1]/name", "apple"); formParams.put("xpath:/order/items/item[1]/quantity", "3"); formParams.put("xpath:/order/items/item[1]/price", ".95"); formParams.put("xpath:/order/items/item[2]/name", "orange"); formParams.put("xpath:/order/items/item[2]/quantity", "5"); formParams.put("xpath:/order/items/item[2]/price", "1.45"); ...
Listing 3: CreateSOAPRequest with unbounded number of elements