tomcat 用AXIS2發布WebService 網站的方法


Axis2+tomcat7.0 實現webService 服務端發布與客戶端的調用。  

Aixs2開發webService的方法有很多,在此只介紹一種比較簡單的實現方法。

第一步:首先要下載開發所需要的jar包   

下載:

axis2-1.6.2-war.zip  http://www.apache.org/dist//axis/axis2/Java/core/1.6.2/ 

下載完后將axis2.war放至tomcat安裝目錄下的webapps文件夾下,然后啟動tomcat后,在webapps目錄下會生成axis2文件夾。  訪問http://localhost:8080/axis2/能看到以下頁面表示axis2運行成功。    

 

第二步  在MyEclipse下新建Web Project,工程名:elecProject。新建包cn.itcast.elec.service,在cn.itcast.elec.service下新建類WebSystemDDLServiceImpl。

[java]  view plain  copy
 
  1. package cn.itcast.elec.service.impl;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.LinkedHashMap;  
  5. import java.util.List;  
  6. import java.util.Map;  
  7.   
  8. import org.apache.commons.lang.StringUtils;  
  9. import org.springframework.context.ApplicationContext;  
  10. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  11.   
  12. import cn.itcast.elec.dao.IElecSystemDDLDao;  
  13. import cn.itcast.elec.domain.ElecSystemDDL;  
  14.   
  15. public class WebSystemDDLServiceImpl {  
  16.   
  17.     public String findSystemByKeyword(String keyword) {  
  18.         ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");  
  19.         IElecSystemDDLDao elecSystemDDLDao = (IElecSystemDDLDao) ac.getBean(IElecSystemDDLDao.SERVICE_NAME);  
  20.           
  21.         //組織查詢條件  
  22.         String condition = "";  
  23.         List<Object> paramsList = new ArrayList<Object>();  
  24.         if(StringUtils.isNotBlank(keyword)){  
  25.             condition += " and o.keyword = ?";  
  26.             paramsList.add(keyword);  
  27.         }  
  28.         Object [] params = paramsList.toArray();  
  29.         //排序語句  
  30.         Map<String, String> orderby = new LinkedHashMap<String, String>();  
  31.         orderby.put("o.ddlCode","asc");//按照數據項的編號升序排列  
  32.         //數據字典進行查詢的時候,使用二級緩存增強檢索的效率  
  33.         List<ElecSystemDDL> list = elecSystemDDLDao.findColectionByConditionNoPageWithCache(condition, params, orderby);  
  34. //      List<ElecSystemDDL> list = elecSystemDDLDao.findColectionByConditionNoPage(condition, params, orderby);  
  35.         StringBuffer webObject  = new StringBuffer("");//axis2支持String類型和XML的類型  
  36.         if(list!=null && list.size()>0){  
  37.             for(int i=0;i<list.size();i++){  
  38.                 webObject.append(list.get(i).getDdlName()+",");//值之間用逗號分隔  
  39.             }  
  40.             webObject.deleteCharAt(webObject.length()-1);  
  41.         }  
  42.         return webObject.toString();  
  43.     }  
  44. }  

 

 

在WEB-INF目錄下修改web.xml文件,內容如下:  

[html]  view plain  copy
 
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <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">       
  3. <!--Axis2 config start-->   
  4. <servlet>    
  5. <servlet-name>AxisServlet</servlet-name>   <servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class >   <load-on-startup>1</load-on-startup>   
  6. </servlet>    
  7. <servlet-mapping>       
  8. <servlet-name>AxisServlet</servlet-name>    <url-pattern>/services/*</url-pattern>     
  9. </servlet-mapping>      
  10. <!--Axis2  end-->     
  11. </web-app>     

把tomcat安裝目錄下的webapps/axis2/WEB-INF下的modules、service和conf文件拷至itcastProject下的WEB-INF目錄下。同時把lib下的如下jar包也拷到項目的lib包下

為了與項目的其他包不發生沖突,需要的jar包有:

 

然后在WEB-INF/services下新建systemDDLService/META-INF路徑,

META-INF下新建services.xml,

內容如下:  

 

[html]  view plain  copy
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <service name="systemDDLService">         
  3.     <description>elecProject Service Example</description>          
  4.     <parameter name="ServiceClass">cn.itcast.elec.service.impl.WebSystemDDLServiceImpl</parameter>    
  5.     <operation name="findSystemByKeyword">             
  6.         <messageReceiver  class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />         
  7.     </operation>  
  8. </service>   



 

 

啟動tomcat后訪問:

http://127.0.0.1:8080/elecProject/services/systemDDLService?wsdl能看到服務信息了。  到此Axis2的WebService服務已成功發布。

看webservice的使用說明書,記住要從下向上看。

(1)

 

(2)

 

(3)

 

(4)

 

(5)

 

Axis2客戶端調用:

下面看看利用axis2 客戶端調用實例   

客戶端程序需要的jar包

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM