1、准备jar包,全部来自cxf2.2.6/lib下
2、配置web.xml
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
3、编写代码
interface:UserService
class: UserServiceImpl
entity:User
Dal :UserDao
@WebMethod(operationName="GetUserById")
@WebResult(name="result")
public User getUserById(@WebParam(name="userId") String userId);
@WebMethod(operationName="GetAllUsers")
@WebResult(name="result")
public List<User> getAllUsers();
}
@WebMethod(operationName="GetUserById")
@WebResult(name="result")
public User getUserById(@WebParam(name="userId") String userId);
@WebMethod(operationName="GetAllUsers")
@WebResult(name="result")
public List<User> getAllUsers();
}
@WebMethod(operationName="GetUserById")
@WebResult(name="result")
public User getUserById(@WebParam(name="userId") String userId);
@WebMethod(operationName="GetAllUsers")
@WebResult(name="result")
public List<User> getAllUsers();
}
@WebMethod(operationName="GetUserById")
@WebResult(name="result")
public User getUserById(@WebParam(name="userId") String userId);
@WebMethod(operationName="GetAllUsers")
@WebResult(name="result")
public List<User> getAllUsers();
}
4、编写applicationContext.xml
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<bean id="userDao" class="com.ytsd.dao.UserDao"></bean>
<jaxws:endpoint id="userManager" implementorClass="com.ytsd.service.UserService"
address="/UserManager">
<jaxws:implementor>
<bean id="userServiceImpl" class="com.ytsd.service.impl.UserServiceImpl">
<property name="userDao">
<ref bean="userDao"/>
</property>
</bean>
</jaxws:implementor>
</jaxws:endpoint>
</beans>
5、启动tomcat,
访问:http://127.0.0.1:8080/ytsd/service
发布成功,点击连接可以看到生成的wsdl
6、测试
在web-inf下建立test/testService.html
测试1:调用webservice的Insert方法
==============================================================================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Test UserManager Service</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript">
function post(){
var xmlHttp=false;
if(!xmlHttp && typeof XMLHttpRequest != 'undefined'){
try{
xmlHttp = new XMLHttpRequest();
}catch(e){
xmlHttp = false;
}
}
if(!xmlHttp && window.createRequest){
try{
xmlHttp = window.createRequest();
}catch(e){
xmlHttp = false;
}
}
var dest = document.getElementById('destination').value;
xmlHttp.open("POST",dest,true);
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readState==4){
document.getElementById('result').innerHTML=xmlHttp.responseText;
}
}
xmlHttp.setRequestHeader("Man","POST "+dest+" HTTP/1.1");
xmlHttp.setRequestHeader("MessageType","CALL");
xmlHttp.setRequestHeader("Content-Type","text/xml");
var texta = document.getElementById('texta');
var soapmess = texta.value;
//alert(soapmess);
xmlHttp.send(soapmess);
}
</script>
</head>
<body>
<center>
<h2>
Test UserManager Service
</h2>
<form id="forma">
<textarea rows="10" cols="80" id="texta"><?xml version="1.0" encoding="UTF-8"?><soap-env:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/"><soap-env:Body><Insert><userId>200013</userId><userName>Tom zhang</userName><userEmail>tom@163.com</userEmail><userAge>35</userAge></Insert></soap-env:Body></soap-env:Envelope>
</textarea>
<p></p>
<input type="text" id="destination" size="50" value="http://127.0.0.1:8080/ytsd/service/UserManager">
<p></p>
<input type="button" onclick="post();" value="Submit" id="submit">
</form>
<div id="container">
Result:
<hr/>
<div id="result"></div>
<hr/>
<div id="soap"></div>
</div>
</center>
</body>
</html>
===================================================================
测试2:调用webservice的GetUserById方法
<?xml
version="1.0" encoding="UTF-8"?><soap-env:Envelope
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/"><soap-env:Body><GetUserById><userId>20001</
userId
></
GetUserById
></soap-env:Body></soap-env:Envelope>
测试2:调用webservice的GetAllUsers方法
<?xml
version="1.0" encoding="UTF-8"?><soap-env:Envelope
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/"><soap-env:Body><GetAllUsers>
</GetAllUsers
></soap-env:Body></soap-env:Envelope>