cxf2.2.6+spring2.5.6+tomcat6.0.20创建webService jar



cxf2.2.6+spring2.5.6+tomcat6.0.20创建webService。

1、准备jar包,全部来自cxf2.2.6/lib下

2、配置web.xml

 

[xhtml] view plaincopy

  1. <?xml version=”1.0″ encoding=”UTF-8″?>
  2. <web-app version=”2.5″ xmlns=”http://java.sun.com/xml/ns/javaee”
  3.     xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
  4.     xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee
  5.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd”>
  6.     <servlet>
  7.         <display-name>CXF Servletdisplay</display-name>
  8.         <servlet-name>CXFServlet</servlet-name>
  9.         <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  10.         <load-on-startup>1</load-on-startup>
  11.     </servlet>
  12.     <servlet-mapping>
  13.         <servlet-name>CXFServlet</servlet-name>
  14.         <url-pattern>/service/*</url-pattern>
  15.     </servlet-mapping>
  16.     <context-param>
  17.         <param-name>contextConfigLocation</param-name>
  18.         <param-value>classpath:applicationContext.xml</param-value>
  19.     </context-param>
  20.     <listener>
  21.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  22.     </listener>
  23.     <welcome-file-list>
  24.         <welcome-file>index.jsp</welcome-file>
  25.     </welcome-file-list>
  26. </web-app>

 

3、编写代码

interface:UserService

class: UserServiceImpl

entity:User

Dal :UserDao

 

  1. package com.ytsd.service;
  2. import java.util.List;
  3. import javax.jws.WebMethod;
  4. import javax.jws.WebParam;
  5. import javax.jws.WebResult;
  6. import javax.jws.WebService;
  7. import com.ytsd.entity.User;
  8. @WebService(targetNamespace=”http://127.0.0.1:8080/client”)
  9. public interface UserService {
  10.     @WebMethod(operationName=”Insert”)
  11.     public void insert(
  12.         @WebParam(name=”userId”)    String userId,
  13.         @WebParam(name=”userName”)  String userName,
  14.         @WebParam(name=”userEmail”) String userEmail,
  15.         @WebParam(name=”userAge”)   int userAge
  16.     );
  17.     @WebMethod(operationName=”GetUserById”)
  18.     @WebResult(name=”result”)
  19.     public User getUserById(@WebParam(name=”userId”) String userId);
  20.     @WebMethod(operationName=”GetAllUsers”)
  21.     @WebResult(name=”result”)
  22.     public List<User> getAllUsers();
  23. }

 

 

  1. package com.ytsd.service;
  2. import java.util.List;
  3. import javax.jws.WebMethod;
  4. import javax.jws.WebParam;
  5. import javax.jws.WebResult;
  6. import javax.jws.WebService;
  7. import com.ytsd.entity.User;
  8. @WebService(targetNamespace=”http://127.0.0.1:8080/client”)
  9. public interface UserService {
  10.     @WebMethod(operationName=”Insert”)
  11.     public void insert(
  12.         @WebParam(name=”userId”)    String userId,
  13.         @WebParam(name=”userName”)  String userName,
  14.         @WebParam(name=”userEmail”) String userEmail,
  15.         @WebParam(name=”userAge”)   int userAge
  16.     );
  17.     @WebMethod(operationName=”GetUserById”)
  18.     @WebResult(name=”result”)
  19.     public User getUserById(@WebParam(name=”userId”) String userId);
  20.     @WebMethod(operationName=”GetAllUsers”)
  21.     @WebResult(name=”result”)
  22.     public List<User> getAllUsers();
  23. }

 

 

  1. package com.ytsd.service;
  2. import java.util.List;
  3. import javax.jws.WebMethod;
  4. import javax.jws.WebParam;
  5. import javax.jws.WebResult;
  6. import javax.jws.WebService;
  7. import com.ytsd.entity.User;
  8. @WebService(targetNamespace=”http://127.0.0.1:8080/client”)
  9. public interface UserService {
  10.     @WebMethod(operationName=”Insert”)
  11.     public void insert(
  12.         @WebParam(name=”userId”)    String userId,
  13.         @WebParam(name=”userName”)  String userName,
  14.         @WebParam(name=”userEmail”) String userEmail,
  15.         @WebParam(name=”userAge”)   int userAge
  16.     );
  17.     @WebMethod(operationName=”GetUserById”)
  18.     @WebResult(name=”result”)
  19.     public User getUserById(@WebParam(name=”userId”) String userId);
  20.     @WebMethod(operationName=”GetAllUsers”)
  21.     @WebResult(name=”result”)
  22.     public List<User> getAllUsers();
  23. }

 

 

  1. package com.ytsd.service;
  2. import java.util.List;
  3. import javax.jws.WebMethod;
  4. import javax.jws.WebParam;
  5. import javax.jws.WebResult;
  6. import javax.jws.WebService;
  7. import com.ytsd.entity.User;
  8. @WebService(targetNamespace=”http://127.0.0.1:8080/client”)
  9. public interface UserService {
  10.     @WebMethod(operationName=”Insert”)
  11.     public void insert(
  12.         @WebParam(name=”userId”)    String userId,
  13.         @WebParam(name=”userName”)  String userName,
  14.         @WebParam(name=”userEmail”) String userEmail,
  15.         @WebParam(name=”userAge”)   int userAge
  16.     );
  17.     @WebMethod(operationName=”GetUserById”)
  18.     @WebResult(name=”result”)
  19.     public User getUserById(@WebParam(name=”userId”) String userId);
  20.     @WebMethod(operationName=”GetAllUsers”)
  21.     @WebResult(name=”result”)
  22.     public List<User> getAllUsers();
  23. }

 

4、编写applicationContext.xml

 

[xhtml] view plaincopy

  1. <beans xmlns=”http://www.springframework.org/schema/beans”
  2.     xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
  3.     xmlns:jaxws=”http://cxf.apache.org/jaxws”
  4.     xsi:schemaLocation=”http://www.springframework.org/schema/beans
  5.         http://www.springframework.org/schema/beans/spring-beans.xsd
  6.         http://cxf.apache.org/jaxws
  7.         http://cxf.apache.org/schemas/jaxws.xsd”>
  8.     <import resource=”classpath:META-INF/cxf/cxf.xml” />
  9.     <import resource=”classpath:META-INF/cxf/cxf-extension-soap.xml” />
  10.     <import resource=”classpath:META-INF/cxf/cxf-servlet.xml” />
  11.     <bean id=”userDao” class=”com.ytsd.dao.UserDao”></bean>
  12.     <jaxws:endpoint id=”userManager” implementorClass=”com.ytsd.service.UserService”
  13.         address=”/UserManager”>
  14.         <jaxws:implementor>
  15.             <bean id=”userServiceImpl” class=”com.ytsd.service.impl.UserServiceImpl”>
  16.                 <property name=”userDao”>
  17.                     <ref bean=”userDao”/>
  18.                 </property>
  19.             </bean>
  20.         </jaxws:implementor>
  21.     </jaxws:endpoint>
  22. </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>