cxf2.2.6+spring2.5.6+tomcat6.0.20创建webService。
1、准备jar包,全部来自cxf2.2.6/lib下
2、配置web.xml
- <?xml version=”1.0″ encoding=”UTF-8″?>
- <web-app version=”2.5″ xmlns=”http://java.sun.com/xml/ns/javaee”
- xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
- xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd”>
- <servlet>
- <display-name>CXF Servletdisplay</display-name>
- <servlet-name>CXFServlet</servlet-name>
- <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>CXFServlet</servlet-name>
- <url-pattern>/service/*</url-pattern>
- </servlet-mapping>
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:applicationContext.xml</param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
3、编写代码
interface:UserService
class: UserServiceImpl
entity:User
Dal :UserDao
- package com.ytsd.service;
- import java.util.List;
- import javax.jws.WebMethod;
- import javax.jws.WebParam;
- import javax.jws.WebResult;
- import javax.jws.WebService;
- import com.ytsd.entity.User;
- @WebService(targetNamespace=”http://127.0.0.1:8080/client”)
- public interface UserService {
- @WebMethod(operationName=”Insert”)
- public void insert(
- @WebParam(name=”userId”) String userId,
- @WebParam(name=”userName”) String userName,
- @WebParam(name=”userEmail”) String userEmail,
- @WebParam(name=”userAge”) int userAge
- );
- @WebMethod(operationName=”GetUserById”)
- @WebResult(name=”result”)
- public User getUserById(@WebParam(name=”userId”) String userId);
- @WebMethod(operationName=”GetAllUsers”)
- @WebResult(name=”result”)
- public List<User> getAllUsers();
- }
- package com.ytsd.service;
- import java.util.List;
- import javax.jws.WebMethod;
- import javax.jws.WebParam;
- import javax.jws.WebResult;
- import javax.jws.WebService;
- import com.ytsd.entity.User;
- @WebService(targetNamespace=”http://127.0.0.1:8080/client”)
- public interface UserService {
- @WebMethod(operationName=”Insert”)
- public void insert(
- @WebParam(name=”userId”) String userId,
- @WebParam(name=”userName”) String userName,
- @WebParam(name=”userEmail”) String userEmail,
- @WebParam(name=”userAge”) int userAge
- );
- @WebMethod(operationName=”GetUserById”)
- @WebResult(name=”result”)
- public User getUserById(@WebParam(name=”userId”) String userId);
- @WebMethod(operationName=”GetAllUsers”)
- @WebResult(name=”result”)
- public List<User> getAllUsers();
- }
- package com.ytsd.service;
- import java.util.List;
- import javax.jws.WebMethod;
- import javax.jws.WebParam;
- import javax.jws.WebResult;
- import javax.jws.WebService;
- import com.ytsd.entity.User;
- @WebService(targetNamespace=”http://127.0.0.1:8080/client”)
- public interface UserService {
- @WebMethod(operationName=”Insert”)
- public void insert(
- @WebParam(name=”userId”) String userId,
- @WebParam(name=”userName”) String userName,
- @WebParam(name=”userEmail”) String userEmail,
- @WebParam(name=”userAge”) int userAge
- );
- @WebMethod(operationName=”GetUserById”)
- @WebResult(name=”result”)
- public User getUserById(@WebParam(name=”userId”) String userId);
- @WebMethod(operationName=”GetAllUsers”)
- @WebResult(name=”result”)
- public List<User> getAllUsers();
- }
- package com.ytsd.service;
- import java.util.List;
- import javax.jws.WebMethod;
- import javax.jws.WebParam;
- import javax.jws.WebResult;
- import javax.jws.WebService;
- import com.ytsd.entity.User;
- @WebService(targetNamespace=”http://127.0.0.1:8080/client”)
- public interface UserService {
- @WebMethod(operationName=”Insert”)
- public void insert(
- @WebParam(name=”userId”) String userId,
- @WebParam(name=”userName”) String userName,
- @WebParam(name=”userEmail”) String userEmail,
- @WebParam(name=”userAge”) int userAge
- );
- @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
- <beans xmlns=”http://www.springframework.org/schema/beans”
- xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
- xmlns:jaxws=”http://cxf.apache.org/jaxws”
- xsi:schemaLocation=”http://www.springframework.org/schema/beans
- 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>