Developing a dispatch client that uses SAAJ

import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPBinding;
public class HelloSaajClient {
private static final String TNS = "http://hello.itso/";
public static void main(String... args) throws Exception {
// Define the service name, port name, and endpoint address
QName serviceName = new QName(TNS, "HelloMessengerService");
QName portName = new QName(TNS, "HelloMessenger");
String endpoint = "http://localhost:80/Hello";
// Create a service that can bind to the HelloMessenger port
Service service = Service.create(serviceName);
service.addPort(portName, SOAPBinding.SOAP11HTTP_BINDING,
endpoint);
// Create a Dynamic Dispatch client
Dispatch<SOAPMessage> dispatch = service.createDispatch(portName,
SOAPMessage.class, Service.Mode.MESSAGE);
// Grab the SOAPBinding which has a SAAJ MessageFactory
BindingProvider bindingProvider = (BindingProvider) dispatch;
SOAPBinding binding = (SOAPBinding) bindingProvider.getBinding();

// Use the SAAJ API to create the request
MessageFactory factory = binding.getMessageFactory();
SOAPMessage requestMessage = factory.createMessage();
SOAPBody soapBody = requestMessage.getSOAPBody();
QName payloadRootElem = new QName(TNS, "sayHello", "h");
SOAPBodyElement bodyElement =
soapBody.addBodyElement(payloadRootElem);
bodyElement.addChildElement("arg0").addTextNode("Milo");
// Invoke the HelloMessenger Web service
SOAPMessage responseMessage = dispatch.invoke(requestMessage);
// Convert the response message
String response = responseMessage.getSOAPBody().getTextContent();
// Print the response
System.out.println(response);
}
}

 

你可能感兴趣的:(xml,Web,SOAP)