操作循序:
此顺序基于契约优先原则,在我之前写过的webservice(六)的基础上进行修改
1.将wsdl拷贝到webContent下的WEB-INF中(导入JAX-RI包)
在wsdl中就可以使用include方式来导入了(暂时先不要进行导入,经测试发现导入可能引发问题)
修改实现类中的wsdlLocation的目录名
2.在web-inf下创建一个文件名为:sun-jaxws.xml
并进行如下修改:
<?xml version="1.0" encoding="UTF-8"?>
<endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"
version="2.0">
<!-- 1.Servlet中所需要对应的名称2.实现类3.浏览器的后缀 -->
<endpoint name="UserService" implementation="cn.edu.zttc.service.UserServiceImpl"
url-pattern="/us" />
</endpoints>
3.配置web.xml
加入监听器
<listener>
<listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>
<servlet>
<servlet-name>UserService</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserService</servlet-name>
<url-pattern>/us</url-pattern>
</servlet-mapping>
具体实例如下:
服务器端:
user.wsdl
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://service.zttc.edu.cn" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="UserService" targetNamespace="http://service.zttc.edu.cn"> <!-- wrappered 推荐使用的方式 --> <wsdl:types> <xsd:schema targetNamespace="http://service.zttc.edu.cn"> <!-- import是在导入的时候加入了命名空间 --> <!-- <xsd:import namespace="http://service.zttc.edu.cn" schemaLocation="user.xsd"/> --> <!-- include是在当前的schema中加入了命名空间 --> <!-- <xsd:include schemaLocation="user.xsd"/> --> <xsd:element name="add" type="tns:add"></xsd:element> <xsd:element name="addResponse" type="tns:addResponse"></xsd:element> <xsd:element name="delete" type="tns:delete"></xsd:element> <xsd:element name="deleteResponse" type="tns:deleteResponse"></xsd:element> <xsd:element name="list" type="tns:list"></xsd:element> <xsd:element name="listResponse" type="tns:listResponse"></xsd:element> <xsd:element name="login" type="tns:login"></xsd:element> <xsd:element name="loginResponse" type="tns:loginResponse"></xsd:element> <!-- 1.添加异常 --> <xsd:element name="UserRunTimeException" type="tns:UserRunTimeException"></xsd:element> <xsd:complexType name="UserRunTimeException"> <xsd:sequence> <xsd:element name="message" type="xsd:string"></xsd:element> </xsd:sequence> </xsd:complexType> <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="1" 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:element name="user" type="tns:user"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name="user"> <xsd:sequence> <xsd:element name="username" type="xsd:string"/> <xsd:element name="password" type="xsd:string"/> <xsd:element name="nickname" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:schema> </wsdl:types> <wsdl:message name="add"> <wsdl:part element="tns:add" name="parameters" /> </wsdl:message> <wsdl:message name="addResponse"> <wsdl:part element="tns:addResponse" name="parameters" /> </wsdl:message> <wsdl:message name="delete"> <wsdl:part element="tns:delete" name="parameters" /> </wsdl:message> <wsdl:message name="deleteResponse"> <wsdl:part element="tns:deleteResponse" name="parameters" /> </wsdl:message> <wsdl:message name="list"> <wsdl:part element="tns:list" name="parameters" /> </wsdl:message> <wsdl:message name="listResponse"> <wsdl:part element="tns:listResponse" name="parameters" /> </wsdl:message> <wsdl:message name="login"> <wsdl:part element="tns:login" name="parameters" /> </wsdl:message> <wsdl:message name="loginResponse"> <wsdl:part element="tns:loginResponse" name="parameters" /> </wsdl:message> <!-- 2.创建异常消息 --> <wsdl:message name="UserRunTimeException"> <wsdl:part element="tns:UserRunTimeException" name="fault" /> </wsdl:message> <wsdl:portType name="IUserService"> <wsdl:operation name="add"> <wsdl:input message="tns:add" /> <wsdl:output message="tns:addResponse" /> <!-- 3.为方法设置异常 --> <wsdl:fault name="UserRunTimeException" message="tns:UserRunTimeException"/> </wsdl:operation> <wsdl:operation name="delete"> <wsdl:input message="tns:delete" /> <wsdl:output message="tns:deleteResponse" /> </wsdl:operation> <wsdl:operation name="list"> <wsdl:input message="tns:list" /> <wsdl:output message="tns:listResponse" /> </wsdl:operation> <wsdl:operation name="login"> <wsdl:input message="tns:login" /> <wsdl:output message="tns:loginResponse" /> <!-- 3.为方法设置异常 --> <wsdl:fault name="UserRunTimeException" message="tns:UserRunTimeException"/> </wsdl:operation> </wsdl:portType> <wsdl:binding name="userServiceSOAP" type="tns:IUserService"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" /> <wsdl:operation name="add"> <wsdl:input> <soap:body use="literal" /> </wsdl:input> <wsdl:output> <soap:body use="literal" /> </wsdl:output> <!-- 4.进行异常的fault绑定 --> <wsdl:fault name="UserRunTimeException"> <soap:fault name="UserRunTimeException" use="literal"/> </wsdl:fault> </wsdl:operation> <wsdl:operation name="delete"> <wsdl:input> <soap:body use="literal" /> </wsdl:input> <wsdl:output> <soap:body use="literal" /> </wsdl:output> </wsdl:operation> <wsdl:operation name="list"> <wsdl:input> <soap:body use="literal" /> </wsdl:input> <wsdl:output> <soap:body use="literal" /> </wsdl:output> </wsdl:operation> <wsdl:operation name="login"> <wsdl:input> <soap:body use="literal" /> </wsdl:input> <wsdl:output> <soap:body use="literal" /> </wsdl:output> <!-- 4.进行异常的fault绑定 --> <wsdl:fault name="UserRunTimeException"> <soap:fault name="UserRunTimeException" use="literal"/> </wsdl:fault> </wsdl:operation> </wsdl:binding> <!-- 在当前文件的最上面的name中填写的 --> <wsdl:service name="UserService"> <wsdl:port binding="tns:userServiceSOAP" name="userServicePort"> <soap:address location="http://localhost:8080/web-soa/us" /> </wsdl:port> </wsdl:service> </wsdl:definitions>
package cn.edu.zttc.service; import java.util.List; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; import javax.xml.ws.RequestWrapper; import javax.xml.ws.ResponseWrapper; import org.zttc.dao.UserRunTimeException; import org.zttc.vo.User; /** * This class was generated by the JAX-WS RI. * JAX-WS RI 2.2.4-b01 * Generated source version: 2.2 * */ @WebService(name = "IUserService", targetNamespace = "http://service.zttc.edu.cn") public interface IUserService { /** * * @param user * @throws UserRunTimeException_Exception */ @WebMethod @RequestWrapper(localName = "add", targetNamespace = "http://service.zttc.edu.cn", className = "cn.edu.zttc.service.Add") @ResponseWrapper(localName = "addResponse", targetNamespace = "http://service.zttc.edu.cn", className = "cn.edu.zttc.service.AddResponse") public void add( @WebParam(name = "user", targetNamespace = "") User user) throws UserRunTimeException ; /** * * @param username */ @WebMethod @RequestWrapper(localName = "delete", targetNamespace = "http://service.zttc.edu.cn", className = "cn.edu.zttc.service.Delete") @ResponseWrapper(localName = "deleteResponse", targetNamespace = "http://service.zttc.edu.cn", className = "cn.edu.zttc.service.DeleteResponse") public void delete( @WebParam(name = "username", targetNamespace = "") String username); /** * * @return * returns java.util.List<cn.edu.zttc.service.User> */ @WebMethod @WebResult(name = "user", targetNamespace = "") @RequestWrapper(localName = "list", targetNamespace = "http://service.zttc.edu.cn", className = "cn.edu.zttc.service.List") @ResponseWrapper(localName = "listResponse", targetNamespace = "http://service.zttc.edu.cn", className = "cn.edu.zttc.service.ListResponse") public List<User> list(); /** * * @param username * @param password * @return * returns cn.edu.zttc.service.User * @throws UserRunTimeException_Exception */ @WebMethod @WebResult(name = "user", targetNamespace = "") @RequestWrapper(localName = "login", targetNamespace = "http://service.zttc.edu.cn", className = "cn.edu.zttc.service.Login") @ResponseWrapper(localName = "loginResponse", targetNamespace = "http://service.zttc.edu.cn", className = "cn.edu.zttc.service.LoginResponse") public User login( @WebParam(name = "username", targetNamespace = "") String username, @WebParam(name = "password", targetNamespace = "") String password) throws UserRunTimeException ; }
package cn.edu.zttc.service; import java.util.List; import javax.jws.WebService; import org.zttc.dao.UserDao; import org.zttc.dao.UserRunTimeException; import org.zttc.vo.User; @WebService(endpointInterface="cn.edu.zttc.service.IUserService", wsdlLocation="WEB-INF/wsdl/user.wsdl", serviceName="UserService", portName="userServicePort", targetNamespace="http://service.zttc.edu.cn") public class UserServiceImpl implements IUserService { private UserDao userDao = UserDao.newInstence(); @Override public void add(User user) throws UserRunTimeException { userDao.add(user); } @Override public void delete(String username) { userDao.delete(username); } @Override public List<User> list() { return userDao.list(); } @Override public User login(String username, String password) throws UserRunTimeException { return userDao.login(username, password); } }
package org.zttc.dao; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import org.zttc.vo.User; public class UserDao { private static final Map<String,User> users = new HashMap<String, User>(); private static UserDao dao = null; public UserDao(){ users.put("admin", new User("admin", "admin", "超级管理员")); } public static UserDao newInstence(){ if (null==dao)dao = new UserDao(); return dao; } public void add(User user) throws UserRunTimeException{ System.out.println("---------add--------"+user); System.out.println("---------user.getUsername()--------"+user.getUsername()); if (users.containsKey(user.getUsername())) { throw new UserRunTimeException("用户已存在"); } users.put(user.getUsername(), user); } public void delete(String username){ System.out.println("---------delete--------"+username); users.remove(username); } public List<User> list(){ System.out.println("---------list--------"+users); Set<String> keys = users.keySet(); List<User> list = new ArrayList<User>(); for (String key : keys) { list.add(users.get(key)); } return list; } public User login(String username,String password) throws UserRunTimeException{ System.out.println("---------login--------"+username+password); if (!users.containsKey(username)) { throw new UserRunTimeException("用户不存在"); } User u = users.get(username); if (!password.equals(u.getPassword())) { throw new UserRunTimeException("用户密码不正确"); } return u; } }
package org.zttc.dao; /** * 继承RunTimeException会在服务器端抛出异常 * 所以要继承为Exception */ public class UserRunTimeException extends Exception { private static final long serialVersionUID = 1L; public UserRunTimeException() { super(); } public UserRunTimeException(String arg0, Throwable arg1, boolean arg2, boolean arg3) { super(arg0, arg1, arg2, arg3); } public UserRunTimeException(String arg0, Throwable arg1) { super(arg0, arg1); } public UserRunTimeException(String arg0) { super(arg0); } public UserRunTimeException(Throwable arg0) { super(arg0); } }
package org.zttc.vo; public class User { private String username; private String password; private String nickname; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getNickname() { return nickname; } public void setNickname(String nickname) { this.nickname = nickname; } public User(String username, String password, String nickname) { super(); this.username = username; this.password = password; this.nickname = nickname; } public User() { super(); } }
<?xml version="1.0" encoding="UTF-8"?> <endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime" version="2.0"> <!-- 1.Servlet中所需要对应的名称2.实现类3.浏览器的后缀 --> <endpoint name="UserService" implementation="cn.edu.zttc.service.UserServiceImpl" url-pattern="/us" /> </endpoints>
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>07_soa</display-name> <listener> <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class> </listener> <servlet> <servlet-name>UserService</servlet-name> <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>UserService</servlet-name> <url-pattern>/us</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
根据web服务连接(http://localhost:8989/web-soa/us?wsdl)生成后添加测试类:
package org.zttc.test; import java.net.MalformedURLException; import java.net.URL; import java.util.List; import javax.xml.namespace.QName; import org.junit.Before; import org.junit.Test; import cn.edu.zttc.service.IUserService; import cn.edu.zttc.service.User; import cn.edu.zttc.service.UserRunTimeException_Exception; import cn.edu.zttc.service.UserService; public class TestService { private IUserService port; private UserService us; private String ns = "http://service.zttc.edu.cn"; @Before public void init(){ try { URL url = new URL("http://localhost:8989/web-soa/us?wsdl"); QName name = new QName(ns,"UserService"); us = new UserService(url,name); port = us.getUserServicePort(); } catch (MalformedURLException e) { e.printStackTrace(); } } @Test public void testList(){ List<User> list = port.list(); for (User user : list) { System.out.println(user.getNickname()); } } @Test public void testAdd(){ try{ User user = new User(); user.setNickname("搜索"); user.setPassword("456123"); user.setUsername("ss"); port.add(user); }catch (UserRunTimeException_Exception e) { System.out.println(e.getMessage()); } } @Test public void testLogin(){ try{ User u = port.login("ss", "456123"); System.out.println(u.getNickname()); }catch (UserRunTimeException_Exception e) { System.out.println(e.getMessage()); } } @Test public void testDelete(){ port.delete("ss"); } }
list.jsp
<%@page import="cn.edu.zttc.service.User"%> <%@page import="java.util.List"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> 当前用户:${loginUser.nickname} <% List<User> users = (List<User>)request.getAttribute("users"); for(User u:users){ %> <%=u.getNickname()%>-------<%=u.getUsername()%> <a href="user.do?username=<%=u.getUsername()%>&method=delete">删除</a> <% } %> </body> </html>
<%@page import="cn.edu.zttc.service.User"%> <%@page import="java.util.List"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="user.do" method="post"> <input type="hidden" name="method" value="add"> Username:<input type="text" name="username"/><br/> password:<input type="text" name="password"/><br/> nickname:<input type="text" name="nickname"/><br/> <input type="submit"> </form> </body> </html>
<%@page import="cn.edu.zttc.service.User"%> <%@page import="java.util.List"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="user.do" method="post"> <input type="hidden" name="method" value="login"> Username:<input type="text" name="username"/><br/> password:<input type="text" name="password"/><br/> <input type="submit"> </form> </body> </html>