首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

应用JAX-WS的Provider和 Dispatch发布WebService的例子

2013-03-12 
使用JAX-WS的Provider和 Dispatch发布WebService的例子package demo.order.providerimport javax.xml.nam

使用JAX-WS的Provider和 Dispatch发布WebService的例子
package demo.order.provider;import javax.xml.namespace.QName;import javax.xml.soap.MessageFactory;import javax.xml.soap.SOAPElement;import javax.xml.soap.SOAPMessage;import javax.xml.transform.dom.DOMSource;import javax.xml.ws.Provider;import javax.xml.ws.Service;import javax.xml.ws.ServiceMode;import javax.xml.ws.WebServiceProvider;import org.w3c.dom.Node;import org.w3c.dom.NodeList;@WebServiceProvider()@ServiceMode(value = Service.Mode.MESSAGE) public class OrderProcessDOMProvider implements Provider<DOMSource> { public OrderProcessDOMProvider() { } public DOMSource invoke(DOMSource request) { DOMSource response = new DOMSource(); try { MessageFactory factory = MessageFactory.newInstance(); SOAPMessage soapReq = factory.createMessage(); soapReq.getSOAPPart().setContent(request); System.out.println("Incoming Client Request as a DOMSource data in MESSAGE Mode"); soapReq.writeTo(System.out); System.out.println("\n"); Node processOrderNode = soapReq.getSOAPBody().getFirstChild(); Node order = processOrderNode.getChildNodes().item(0); NodeList list = order.getChildNodes(); for(int i = 0 ; i<list.getLength() ; i++){ System.out.println(list.item(i).getNodeName() + "=" + list.item(i).getFirstChild().getNodeValue()); } SOAPMessage orderResponse = factory.createMessage(); QName processOrderQName = new QName("http://order.demo/", "processOrder"); QName responseQName = new QName("http://order.demo/", "return"); SOAPElement processOrderResponse = orderResponse.getSOAPBody().addChildElement(processOrderQName); processOrderResponse.addChildElement(responseQName).addTextNode("ORD1234"); response.setNode(orderResponse.getSOAPPart()); } catch (Exception ex) { ex.printStackTrace(); } return response; }}

?这里的?Provider泛型中可传入javax.xml.transform.Source类型,javax.activation.DataSource类型 和javax.xml.soap.SOAPMessage类型

javax.xml.transform.Source类型又有如下子类

? 1.DOMSource

? 2.SAXSource

? 3.StreamSource

ServiceMode注解来以来指定接收请求报文的模式(Service.Mode.MESSAGE 或?Service.Mode.PAYLOAD)

?

发布服务代码:

package demo.order.provider;import javax.xml.ws.Endpoint;public class Server {    protected Server() throws Exception {        System.out.println("Starting Server");    Object implementor = new OrderProcessDOMProvider();        String address = "http://localhost:8080/OrderProcessDOMProvider";        Endpoint.publish(address, implementor);            }    public static void main(String args[]) throws Exception {        new Server();        System.out.println("Server ready...");        Thread.sleep(5 * 60 * 1000);        System.out.println("Server exiting");        System.exit(0);    }}

?

生成的WSDL:

<?xml version='1.0' encoding='UTF-8'?><wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://provider.order.demo/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="OrderProcessDOMProviderService" targetNamespace="http://provider.order.demo/">  <wsdl:types><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://provider.order.demo/" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://provider.order.demo/">  <xsd:element name="invoke" nillable="true" type="xsd:anyType"/>  <xsd:element name="invokeResponse" nillable="true" type="xsd:anyType"/></xsd:schema>  </wsdl:types>  <wsdl:message name="invokeResponse">    <wsdl:part element="tns:invokeResponse" name="invokeResponse">    </wsdl:part>  </wsdl:message>  <wsdl:message name="invoke">    <wsdl:part element="tns:invoke" name="invoke">    </wsdl:part>  </wsdl:message>  <wsdl:portType name="OrderProcessDOMProvider">    <wsdl:operation name="invoke">      <wsdl:input message="tns:invoke" name="invoke">    </wsdl:input>      <wsdl:output message="tns:invokeResponse" name="invokeResponse">    </wsdl:output>    </wsdl:operation>  </wsdl:portType>  <wsdl:binding name="OrderProcessDOMProviderServiceSoapBinding" type="tns:OrderProcessDOMProvider">    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>    <wsdl:operation name="invoke">      <soap:operation soapAction="" style="document"/>      <wsdl:input name="invoke">        <soap:body use="literal"/>      </wsdl:input>      <wsdl:output name="invokeResponse">        <soap:body use="literal"/>      </wsdl:output>    </wsdl:operation>  </wsdl:binding>  <wsdl:service name="OrderProcessDOMProviderService">    <wsdl:port binding="tns:OrderProcessDOMProviderServiceSoapBinding" name="OrderProcessDOMProviderPort">      <soap:address location="http://localhost:8080/OrderProcessDOMProvider"/>    </wsdl:port>  </wsdl:service></wsdl:definitions>

?客户端代码如下:

package demo.order.dispatcher;import java.net.URL;import javax.xml.namespace.QName;import javax.xml.soap.MessageFactory;import javax.xml.soap.SOAPElement;import javax.xml.soap.SOAPMessage;import javax.xml.transform.dom.DOMSource;import javax.xml.ws.Dispatch;import javax.xml.ws.Service;import javax.xml.ws.Service.Mode;public final class DispatcherClient {public static final String WSDLFile = "http://localhost:8080/OrderProcessDOMProvider?wsdl";    public DispatcherClient() {    }    public static void main(String args[]) throws Exception {URL wsdlURL = new URL(WSDLFile);        MessageFactory factory = MessageFactory.newInstance();        QName domProvider = new QName("http://provider.order.demo/", "OrderProcessDOMProviderService");        QName portName = new QName("http://provider.order.demo/", "OrderProcessDOMProviderPort");        Service service = Service.create(wsdlURL, domProvider);                SOAPMessage soapRequest = factory.createMessage();                        QName processOrderQName = new QName("http://order.demo/", "processOrder");       //create the element - <http://order.demo/:processOrder></http://order.demo/:processOrder>        SOAPElement processOrderResponse = soapRequest.getSOAPBody().addChildElement(processOrderQName);        SOAPElement order = processOrderResponse.addChildElement("arg0");        order.addChildElement("customerID").addTextNode("Naveen");        order.addChildElement("itemID").addTextNode("I001");        order.addChildElement("price").addTextNode("200.00");        order.addChildElement("qty").addTextNode("200");        DOMSource domRequestMsg = new DOMSource(soapRequest.getSOAPPart());        Dispatch<DOMSource> domMsg = service.createDispatch(portName, DOMSource.class, Mode.MESSAGE);        DOMSource domResponseMsg = domMsg.invoke(domRequestMsg);                SOAPMessage soapReq = factory.createMessage();        soapReq.getSOAPPart().setContent(domRequestMsg);        System.out.println("Client Request as a DOMSource data in MESSAGE Mode");        soapReq.writeTo(System.out);        System.out.println("\n");                System.out.println("Response from server: " + domResponseMsg.getNode().getLastChild().getTextContent());        }}

?

热点排行