1、首先编写Wsdl(基于契约优先的方式),要注意的是命名空间(若是使用include或import)最好使用一致的,代码如下:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="http://www.xiaoqiaolv/ws"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
name="UserServiceImpl"
targetNamespace="http://www.xiaoqiaolv/ws">
<wsdl:types>
<xsd:schema targetNamespace="http://www.xiaoqiaolv/ws">
<!-- <xsd:include schemaLocation="UserSchema.xsd"/> -->
<!-- <xsd:import namespace="http://www.xiaoqiaolv/ws" schemaLocation="UserSchema.xsd"/>--> <xsd:element name="add" type="tns:add"/>
<xsd:element name="addResponse" type="tns:addResponse"/>
<xsd:element name="delete" type="tns:delete"/>
<xsd:element name="deleteResponse" type="tns:deleteResponse"/>
<xsd:element name="login" type="tns:login"/>
<xsd:element name="loginResponse" type="tns:loginResponse"/>
<xsd:element name="list" type="tns:list"/>
<xsd:element name="listResponse" type="tns:listResponse"/> <xsd:complexType name="add">
<xsd:sequence>
<xsd:element name="user" type="tns:user"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="addResponse">
<xsd:sequence/>
</xsd:complexType> <xsd:complexType name="delete">
<xsd:sequence>
<xsd:element name="username" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="deleteResponse">
<xsd:sequence/>
</xsd:complexType> <xsd:complexType name="list">
<xsd:sequence/>
</xsd:complexType>
<xsd:complexType name="listResponse">
<xsd:sequence minOccurs="0" maxOccurs="unbounded">
<xsd:element name="user" type="tns:user"/>
</xsd:sequence>
</xsd:complexType> <xsd:complexType name="login">
<xsd:sequence>
<xsd:element name="username" type="xsd:string"/>
<xsd:element name="password" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="loginResponse">
<xsd:sequence/>
</xsd:complexType>
<xsd:complexType name="user">
<xsd:sequence>
<xsd:element name="id" type="xsd:int"/>
<xsd:element name="username" type="xsd:string"/>
<xsd:element name="password" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
</wsdl:types> <wsdl:message name="addMessage">
<wsdl:part element="tns:add" name="add"/>
</wsdl:message>
<wsdl:message name="addResponseMessage">
<wsdl:part element="tns:addResponse" name="addResponse"/>
</wsdl:message> <wsdl:message name="deleteMessage">
<wsdl:part name="delete" element="tns:delete"/>
</wsdl:message>
<wsdl:message name="deleteResponseMessage">
<wsdl:part name="deleteResponse" element="tns:deleteResponse"/>
</wsdl:message> <wsdl:message name="listMessage">
<wsdl:part name="list" element="tns:list"/>
</wsdl:message>
<wsdl:message name="listResponseMessage">
<wsdl:part name="listResponse" element="tns:listResponse"/>
</wsdl:message> <wsdl:message name="loginMessage">
<wsdl:part name="login" element="tns:login"/>
</wsdl:message>
<wsdl:message name="loginResponseMessage">
<wsdl:part name="loginResponse" element="tns:loginResponse"></wsdl:part>
</wsdl:message> <wsdl:portType name="UserService">
<wsdl:operation name="add">
<wsdl:input message="tns:addMessage"/>
<wsdl:output message="tns:addResponseMessage"/>
</wsdl:operation> <wsdl:operation name="delete">
<wsdl:input message="tns:deleteMessage"/>
<wsdl:output message="tns:deleteResponseMessage"/>
</wsdl:operation> <wsdl:operation name="list">
<wsdl:input message="tns:listMessage"/>
<wsdl:output message="tns:listResponseMessage"/>
</wsdl:operation> <wsdl:operation name="login">
<wsdl:input message="tns:loginMessage"/>
<wsdl:output message="tns:loginResponseMessage"/>
</wsdl:operation>
</wsdl:portType> <wsdl:binding name="userBinding" type="tns:UserService">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="add">
<soap:operation soapAction="http://www.xiaoqiaolv/ws/add"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation> <wsdl:operation name="delete">
<soap:operation soapAction="http://www.xiaoqiaolv/ws/delete"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation> <wsdl:operation name="login">
<soap:operation soapAction="http://www.xiaoqiaolv/ws/login"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation> <wsdl:operation name="list">
<soap:operation soapAction="http://www.xiaoqiaolv/ws/list"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="UserServiceImpl">
<wsdl:port binding="tns:userBinding" name="UserServiceImplPort">
<soap:address location="http://localhost:9999/ws"/>
</wsdl:port>
</wsdl:service>
</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、编写测试类:代码如下:
public class Test {
private UserService userService;
private UserServiceImpl userServiceImpl; public void testWsdl(){
try {
URL url = new URL("http://localhost:9999/ws?wsdl");
QName qname = new QName("http://www.xiaoqiaolv/ws","UserServiceImpl");
userServiceImpl = new UserServiceImpl(url,qname);
userService = userServiceImpl.getUserServiceImplPort();
User user = new User();
user.setUsername("aaa");
user.setPassword("1111");
user.setId(21212);
userService.add(user);
List<User> list = userService.list();
for(User u:list){
System.out.println(u.getUsername());
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
public static void main(String[] args) {
new Test().testWsdl();
}
}
6、有问题的话可以打开tcpmon,对接口服务进行监听,查看客户端与服务端传递的信息java -jar tcpmon.jar