webservice(六)基于契约优先的wsdl文件生成webservice服务代码
首先是介绍整个流程 契约优先开发流程 代码如下: ?? mywsdl.wsdl: <?xml version="1.0" encoding="UTF-8" standalone="no"?> <wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://www.example.org/mywsdl/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="mywsdl" targetNamespace="http://www.example.org/mywsdl/"> <wsdl:types> <xsd:schema targetNamespace="http://www.example.org/mywsdl/"> <!-- 定义方法 --> <xsd:element name="add" type="tns:add"/> <xsd:element name="addResponse" type="tns:addResponse"/> <xsd:element name="divide" type="tns:divide"/> <xsd:element name="divideResponse" type="tns:divideResponse"/> <!-- 定义头部信息 --> <xsd:element name="licenseInfo" type="xsd:string"/> <!-- 定义类型 --> <xsd:complexType name="add"> <xsd:sequence> <xsd:element name="a" type="xsd:int"/> <xsd:element name="b" type="xsd:int"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name="addResponse"> <xsd:sequence> <xsd:element name="addResult" type="xsd:int"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name="divide"> <xsd:sequence> <xsd:element name="num1" type="xsd:int"/> <xsd:element name="num2" type="xsd:int"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name="divideResponse"> <xsd:sequence> <xsd:element name="divideResult" type="xsd:int"/> </xsd:sequence> </xsd:complexType> </xsd:schema> </wsdl:types> <wsdl:message name="add"> <wsdl:part name="add" element="tns:add"/> </wsdl:message> <wsdl:message name="addResponse"> <wsdl:part name="addResponse" element="tns:addResponse"/> </wsdl:message> <wsdl:message name="divide"> <wsdl:part name="divide" element="tns:divide"/> </wsdl:message> <wsdl:message name="divideResponse"> <wsdl:part name="divideResponse" element="tns:divideResponse"/> </wsdl:message> <!-- 定义头信息 --> <wsdl:message name="licenseInfo"> <wsdl:part name="licenseInfo" element="tns:licenseInfo"/> </wsdl:message> <wsdl:portType name="IMyService"> <wsdl:operation name="add"> <wsdl:input message="tns:add"/> <wsdl:output message="tns:addResponse"/> </wsdl:operation> <wsdl:operation name="divide"> <wsdl:input message="tns:divide"/> <wsdl:output message="tns:divideResponse"/> </wsdl:operation> </wsdl:portType> <wsdl:binding name="myServiceSOAP" type="tns:IMyService"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="add"> <wsdl:input> <soap:body use="literal"/> <!-- 加入头信息 --> <soap:header use="literal" part="licenseInfo" message="tns:licenseInfo"/> </wsdl:input> <wsdl:output> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> <wsdl:operation name="divide"> <wsdl:input> <soap:body use="literal"/> </wsdl:input> <wsdl:output> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="MyServiceImplService"> <wsdl:port binding="tns:myServiceSOAP" name="MyServiceImplPort"> <soap:address location="http://localhost:8989/ms"/> </wsdl:port> </wsdl:service> </wsdl:definitions> 只留IMyservice类,并进行修改: package org.example.mywsdl; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; import javax.xml.bind.annotation.XmlSeeAlso; import javax.xml.ws.RequestWrapper; import javax.xml.ws.ResponseWrapper; /** * This class was generated by the JAX-WS RI. * JAX-WS RI 2.2.4-b01 * Generated source version: 2.2 * */ @WebService(name = "IMyService",targetNamespace = "http://www.example.org/mywsdl/") public interface IMyService { /** * * @param b * @param a * @return * returns int */ @WebMethod @WebResult(name = "addResult",targetNamespace = "") @RequestWrapper(localName = "add",targetNamespace = "http://www.example.org/mywsdl/",className = "org.example.mywsdl.Add") @ResponseWrapper(localName = "addResponse",className = "org.example.mywsdl.AddResponse") public int add( @WebParam(name = "a",targetNamespace = "") int a,@WebParam(name = "b",targetNamespace = "") int b,@WebParam(name = "licenseInfo",header=true) String licenseInfo); /** * * @param num2 * @param num1 * @return * returns int */ @WebMethod @WebResult(name = "divideResult",targetNamespace = "") @RequestWrapper(localName = "divide",className = "org.example.mywsdl.Divide") @ResponseWrapper(localName = "divideResponse",className = "org.example.mywsdl.DivideResponse") public int divide( @WebParam(name = "num1",targetNamespace = "") int num1,@WebParam(name = "num2",targetNamespace = "") int num2); } 创建实现类之后发布服务 package org.example.mywsdl; import javax.xml.ws.Endpoint; public class MyServer { public static void main(String[] args) { //地址是在wsdl中编写的 Endpoint.publish("http://localhost:8989/ms",new MyServiceImpl()); } } 根据服务器端生成客户端代码后: wsimport -d D:sidworkspace_webservicewsimport -keep -verbose http://localhost:8989/ms?wsdl 加入测试类: package org.example.test; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import javax.xml.namespace.QName; import javax.xml.soap.MessageFactory; import javax.xml.soap.SOAPBody; import javax.xml.soap.SOAPBodyElement; import javax.xml.soap.SOAPEnvelope; import javax.xml.soap.SOAPException; import javax.xml.soap.SOAPHeader; import javax.xml.soap.SOAPMessage; import javax.xml.ws.Dispatch; import javax.xml.ws.Service; public class Test { public static void main(String[] args) { String ns = "http://www.example.org/mywsdl/"; try { QName name = new QName(ns,"MyServiceImplService"); URL url = new URL("http://localhost:8989/ms?wsdl"); // MyServiceImplService mis = new MyServiceImplService(url,name); // IMyService ms = mis.getMyServiceImplPort(); // System.out.println(ms.divide(19,2)); //使用soap来传递消息 Service service = Service.create(url,name); QName pname = new QName(ns,"MyServiceImplPort"); Dispatch<SOAPMessage> dispatch = service.createDispatch(pname,SOAPMessage.class,Service.Mode.MESSAGE); SOAPMessage msg = MessageFactory.newInstance().createMessage(); SOAPEnvelope enve = msg.getSOAPPart().getEnvelope(); SOAPHeader header = enve.getHeader(); SOAPBody body = enve.getBody(); if (header==null) { header = enve.addHeader(); } QName hname = new QName(ns,"licenseInfo","ns"); header.addHeaderElement(hname).setValue("sssssssss"); QName bname = new QName(ns,"add","ns"); SOAPBodyElement ele = body.addBodyElement(bname); ele.addChildElement("a").setValue("12"); ele.addChildElement("b").setValue("12"); msg.writeTo(System.out); System.out.println("n invoking。。。。。。。。"); SOAPMessage rep = dispatch.invoke(msg); rep.writeTo(System.out); } catch (MalformedURLException e) { e.printStackTrace(); } catch (SOAPException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |