1、首先编写Wsdl(基于契约优先的方式),要注意的是命名空间(若是使用include或import)最好使用一致的,代码如下:
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?> 2 <wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 3 xmlns:tns="http://www.xiaoqiaolv/ws" 4 xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 5 xmlns:xsd="http://www.w3.org/2001/XMLSchema" 6 name="UserServiceImpl" 7 targetNamespace="http://www.xiaoqiaolv/ws"> 8 <wsdl:types> 9 <xsd:schema targetNamespace="http://www.xiaoqiaolv/ws"> 10 <!-- <xsd:include schemaLocation="UserSchema.xsd"/> --> 11 <!-- <xsd:import namespace="http://www.xiaoqiaolv/ws" schemaLocation="UserSchema.xsd"/>--> 12 13 <xsd:element name="add" type="tns:add"/> 14 <xsd:element name="addResponse" type="tns:addResponse"/> 15 <xsd:element name="delete" type="tns:delete"/> 16 <xsd:element name="deleteResponse" type="tns:deleteResponse"/> 17 <xsd:element name="login" type="tns:login"/> 18 <xsd:element name="loginResponse" type="tns:loginResponse"/> 19 <xsd:element name="list" type="tns:list"/> 20 <xsd:element name="listResponse" type="tns:listResponse"/> 21 22 <xsd:complexType name="add"> 23 <xsd:sequence> 24 <xsd:element name="user" type="tns:user"/> 25 </xsd:sequence> 26 </xsd:complexType> 27 <xsd:complexType name="addResponse"> 28 <xsd:sequence/> 29 </xsd:complexType> 30 31 <xsd:complexType name="delete"> 32 <xsd:sequence> 33 <xsd:element name="username" type="xsd:string"/> 34 </xsd:sequence> 35 </xsd:complexType> 36 <xsd:complexType name="deleteResponse"> 37 <xsd:sequence/> 38 </xsd:complexType> 39 40 <xsd:complexType name="list"> 41 <xsd:sequence/> 42 </xsd:complexType> 43 <xsd:complexType name="listResponse"> 44 <xsd:sequence minOccurs="0" maxOccurs="unbounded"> 45 <xsd:element name="user" type="tns:user"/> 46 </xsd:sequence> 47 </xsd:complexType> 48 49 <xsd:complexType name="login"> 50 <xsd:sequence> 51 <xsd:element name="username" type="xsd:string"/> 52 <xsd:element name="password" type="xsd:string"/> 53 </xsd:sequence> 54 </xsd:complexType> 55 <xsd:complexType name="loginResponse"> 56 <xsd:sequence/> 57 </xsd:complexType> 58 <xsd:complexType name="user"> 59 <xsd:sequence> 60 <xsd:element name="id" type="xsd:int"/> 61 <xsd:element name="username" type="xsd:string"/> 62 <xsd:element name="password" type="xsd:string"/> 63 </xsd:sequence> 64 </xsd:complexType> 65 </xsd:schema> 66 </wsdl:types> 67 68 <wsdl:message name="addMessage"> 69 <wsdl:part element="tns:add" name="add"/> 70 </wsdl:message> 71 <wsdl:message name="addResponseMessage"> 72 <wsdl:part element="tns:addResponse" name="addResponse"/> 73 </wsdl:message> 74 75 <wsdl:message name="deleteMessage"> 76 <wsdl:part name="delete" element="tns:delete"/> 77 </wsdl:message> 78 <wsdl:message name="deleteResponseMessage"> 79 <wsdl:part name="deleteResponse" element="tns:deleteResponse"/> 80 </wsdl:message> 81 82 <wsdl:message name="listMessage"> 83 <wsdl:part name="list" element="tns:list"/> 84 </wsdl:message> 85 <wsdl:message name="listResponseMessage"> 86 <wsdl:part name="listResponse" element="tns:listResponse"/> 87 </wsdl:message> 88 89 <wsdl:message name="loginMessage"> 90 <wsdl:part name="login" element="tns:login"/> 91 </wsdl:message> 92 <wsdl:message name="loginResponseMessage"> 93 <wsdl:part name="loginResponse" element="tns:loginResponse"></wsdl:part> 94 </wsdl:message> 95 96 <wsdl:portType name="UserService"> 97 <wsdl:operation name="add"> 98 <wsdl:input message="tns:addMessage"/> 99 <wsdl:output message="tns:addResponseMessage"/> 100 </wsdl:operation> 101 102 <wsdl:operation name="delete"> 103 <wsdl:input message="tns:deleteMessage"/> 104 <wsdl:output message="tns:deleteResponseMessage"/> 105 </wsdl:operation> 106 107 <wsdl:operation name="list"> 108 <wsdl:input message="tns:listMessage"/> 109 <wsdl:output message="tns:listResponseMessage"/> 110 </wsdl:operation> 111 112 <wsdl:operation name="login"> 113 <wsdl:input message="tns:loginMessage"/> 114 <wsdl:output message="tns:loginResponseMessage"/> 115 </wsdl:operation> 116 </wsdl:portType> 117 118 <wsdl:binding name="userBinding" type="tns:UserService"> 119 <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> 120 <wsdl:operation name="add"> 121 <soap:operation soapAction="http://www.xiaoqiaolv/ws/add"/> 122 <wsdl:input> 123 <soap:body use="literal"/> 124 </wsdl:input> 125 <wsdl:output> 126 <soap:body use="literal"/> 127 </wsdl:output> 128 </wsdl:operation> 129 130 <wsdl:operation name="delete"> 131 <soap:operation soapAction="http://www.xiaoqiaolv/ws/delete"/> 132 <wsdl:input> 133 <soap:body use="literal"/> 134 </wsdl:input> 135 <wsdl:output> 136 <soap:body use="literal"/> 137 </wsdl:output> 138 </wsdl:operation> 139 140 <wsdl:operation name="login"> 141 <soap:operation soapAction="http://www.xiaoqiaolv/ws/login"/> 142 <wsdl:input> 143 <soap:body use="literal"/> 144 </wsdl:input> 145 <wsdl:output> 146 <soap:body use="literal"/> 147 </wsdl:output> 148 </wsdl:operation> 149 150 <wsdl:operation name="list"> 151 <soap:operation soapAction="http://www.xiaoqiaolv/ws/list"/> 152 <wsdl:input> 153 <soap:body use="literal"/> 154 </wsdl:input> 155 <wsdl:output> 156 <soap:body use="literal"/> 157 </wsdl:output> 158 </wsdl:operation> 159 </wsdl:binding> 160 <wsdl:service name="UserServiceImpl"> 161 <wsdl:port binding="tns:userBinding" name="UserServiceImplPort"> 162 <soap:address location="http://localhost:9999/ws"/> 163 </wsdl:port> 164 </wsdl:service> 165 </wsdl:definitions>
2、实现服务器端的应用实现,这个就不加代码了,根据wsdl文档用wsimport生成服务,只取出接口service,然后其他的和我们正常的web开发都一致。
3、服务端发布服务:public static void main(String[] args) {
Endpoint.publish("http://localhost:9999/ws", new UserServiceImpl());
}
4、实现客户端:根据服务网址http://localhost:9999/ws,生成服务,得到所有以.java为后缀的类,把整个包包含在自己的项目中
5、编写测试类:代码如下:
1 public class Test { 2 private UserService userService; 3 private UserServiceImpl userServiceImpl; 4 5 public void testWsdl(){ 6 try { 7 URL url = new URL("http://localhost:9999/ws?wsdl"); 8 QName qname = new QName("http://www.xiaoqiaolv/ws","UserServiceImpl"); 9 userServiceImpl = new UserServiceImpl(url,qname); 10 userService = userServiceImpl.getUserServiceImplPort(); 11 User user = new User(); 12 user.setUsername("aaa"); 13 user.setPassword("1111"); 14 user.setId(21212); 15 userService.add(user); 16 List<User> list = userService.list(); 17 for(User u:list){ 18 System.out.println(u.getUsername()); 19 } 20 } catch (MalformedURLException e) { 21 // TODO Auto-generated catch block 22 e.printStackTrace(); 23 } 24 25 } 26 public static void main(String[] args) { 27 new Test().testWsdl(); 28 } 29 }
6、有问题的话可以打开tcpmon,对接口服务进行监听,查看客户端与服务端传递的信息java -jar tcpmon.jar
我很小我很弱,我在慢慢成长!