论坛首页 Java企业应用论坛

Developing a dispatch client that uses SAAJ

浏览 2013 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-11-25  
SOA
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);
}
}

 

论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics