Java Reference
In-Depth Information
QName isbnQName = new QName(iNs, elementName);
//Add the <isbn> element to the SOAP body
//as its only child
body.addBodyElement(isbnQName).setValue("12345");
//debug print what we're sending
soapMsg.writeTo(out);
out.println("\nInvoking...");
//send the message as request to service and get response
SOAPMessage response = dispatch.invoke(soapMsg);
//just show in the console for now
response.writeTo(System.out);
} catch (Exception ex) {
ex.printStackTrace();
}
}
The main focus of this example is the creation of the SOAPMessage itself. I included the sur-
rounding Dispatch creation and invocation because there are so many different possible op-
tions to use, and it's frustrating to have short fragments without enough context for you to
recreate the example. But the main point here is that if you have a string you want to use to
pass into a web service, you can do it pretty quickly by following these basic steps:
1. Create Dispatch<SOAPMessage> .
2. Create a SOAPMessage instance from the factory.
3. Get the SOAPPart from the message and use it to get the envelope and body.
4. Create a QName for your element matching the expected namespace for that element given
its schema type.
5. Invoke the addBodyElement method, pass it the QName , and set the element value.
6. Invoke your dispatch with the SOAP message to send the request.
This example uses the previously defined catalog web service that lets you pass an ISBN num-
ber and returns the matching book object. In that example, there are two namespaces: the one
used by the web service itself, and the one that matches the schema that defines the paramet-
ers and return types. The parameter namespace must then be used when creating the qualified
name object for the <isbn> element.
Search WWH ::




Custom Search