使用xfire+spring編寫webservice例子

 

利用xfire編寫webservice的例子,內容如下

1. xfire + spring 發佈webservice
2. 利用 javascript  調用發佈的webservice

使用xfire+spring發佈webservice其實很簡單,遵循一下幾個步驟即可

1. 想要發佈成文webservice的類,必須實現接口
2. 3個配置文件(後面詳細說)

下面針對以上步驟詳細說明
關於1中的要求,給個例子就明白了

Itest.java 代碼
 
  1. package test;  
  2.   
  3. import org.json.JSONException;  
  4.   
  5. public interface IHello  
  6. {  
  7.     public String hello();  
  8.     public String helloTo(String name);  
  9.     public String getJsonData(String pageIndex,String pageSize);  
  10. }  


    

HelloImpl.java 代碼
 
  1. package test;  
  2.   
  3. import java.util.*;  
  4.   
  5. import javax.servlet.http.HttpServletRequest;  
  6. import javax.servlet.http.HttpSession;  
  7.   
  8. import org.codehaus.xfire.transport.http.XFireServletController;  
  9. import org.json.JSONException;  
  10. import org.json.JSONStringer;  
  11.   
  12. public class HelloImpl implements IHello  
  13. {  
  14.   
  15.     public String hello()  
  16.     {  
  17.     return "hello";  
  18.     }  
  19.   
  20.     public String helloTo(String name)  
  21.     {  
  22.     return " hello " + name + "!";  
  23.     }  
  24.   
  25.     private void example()  
  26.     {  
  27.     //使用session  
  28.      HttpServletRequest request = XFireServletController.getRequest();  
  29.      HttpSession session = request.getSession();  
  30.     }  
  31.   
  32.     public String getJsonData(String pageIndex,String pageSize)   
  33.     {  
  34.     String rtnValue = "";  
  35.       
  36.     rtnValue = "";  
  37.     for(int i=0;i<Integer.parseInt(pageSize);i++)  
  38.     {  
  39.         rtnValue = rtnValue + "{'lastname': 'last" + pageIndex + "-" + i + "', 'firstname': 'first" + pageIndex + "-" + i + "' },";  
  40.     }  
  41.     rtnValue = rtnValue + "{'lastname': 'last', 'firstname': 'last' }";  
  42.       
  43.     rtnValue = "{'context':["  + rtnValue + "],'pager':[{'totalRecord':'100','totalpage':'10','pageIndex':'" + pageIndex + "','pageSize':'10'}]}";  
  44.       
  45.     return rtnValue;  
  46.     }  
  47. }  


就這樣寫就可以了

關於三個配置文件

web.xml修改如下

xml 代碼
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  3.   <!-- 配置文件路徑 開始 -->  
  4.     <context-param>  
  5.         <param-name>log4jConfigLocation</param-name>  
  6.         <param-value>/WEB-INF/classes/log4j.properties</param-value>  
  7.     </context-param>  
  8.     <context-param>  
  9.         <param-name>contextConfigLocation</param-name>  
  10.         <param-value>  
  11.             /WEB-INF/classes/applicationContext*.xml  
  12.             classpath:org/codehaus/xfire/spring/xfire.xml  
  13.         </param-value>  
  14.     </context-param>  
  15.       
  16.     <!-- 啓動時加載SpringContextServlet -->  
  17.     <listener>  
  18.         <listener-class>  
  19.             org.springframework.web.context.ContextLoaderListener  
  20.         </listener-class>  
  21.     </listener>  
  22.     <listener>   
  23.         <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>   
  24.     </listener>   
  25.   
  26.     <listener>  
  27.         <listener-class>  
  28.             org.springframework.web.util.IntrospectorCleanupListener  
  29.         </listener-class>  
  30.     </listener>  
  31.       
  32.       <!-- XFire 配置 -->  
  33.       <servlet>   
  34.         <servlet-name>xfire</servlet-name>   
  35.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>   
  36.       </servlet>   
  37.         
  38.       <servlet-mapping>  
  39.         <servlet-name>xfire</servlet-name>  
  40.         <url-pattern>*.ws</url-pattern>  
  41.       </servlet-mapping>  
  42.         
  43.       <welcome-file-list>  
  44.         <welcome-file>index.html</welcome-file>  
  45.       </welcome-file-list>  
  46. </web-app>  


這裏注意   classpath:org/codehaus/xfire/spring/xfire.xml  必須要寫進去

xfire-servlet.xml 新建這個文件,並且和web.xml放在同一個文件夾
注意: 名稱和位置都不能變(也許可以改,我不知道)

xml 代碼
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">  
  3. <beans>   
  4.     <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">   
  5.         <property name="urlMap">  
  6.             <map>  
  7.                 <entry key="/testService.ws">  
  8.                     <ref bean="test"/>    
  9.                 </entry>   
  10.             </map>   
  11.         </property>  
  12.       </bean>   
  13.         
  14.       <bean id="test" parent="webService" class="org.codehaus.xfire.spring.remoting.XFireExporter">   
  15.         <property name="serviceBean">   
  16.             <ref bean="testBean"/>   
  17.         </property>   
  18.         <property name="serviceClass">   
  19.             <value>test.IHello</value>   
  20.         </property>   
  21.       </bean>   
  22.           
  23.       <!-- webService base -->  
  24.       <bean id="webService" class="org.codehaus.xfire.spring.remoting.XFireExporter" abstract="true">  
  25.         <property name="serviceFactory">  
  26.             <ref bean="xfire.serviceFactory" />  
  27.         </property>  
  28.         <property name="xfire">  
  29.             <ref bean="xfire" />  
  30.         </property>  
  31.      </bean>  
  32. </beans>  

spring 的配置文件 applicationContext-webService.xml
xml 代碼
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">    
  3. <beans>  
  4.     <bean id="testBean" class="test.HelloImpl"></bean>  
  5. </beans>  

查看wsdl    http://localhost:8080/mootools/testService.ws?wsdl

運行 進入 index.html 頁面,點擊 即可執行調用,正常顯示錶示發佈成功,調用失敗會返回error(運行前請修改index.html文件的源碼,將url修改成真正的url)

例子很簡單,不過多解釋,源碼查看附件
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章