利用xfire編寫webservice的例子,內容如下
1. xfire + spring 發布webservice
2. 利用 javascript 調用發布的webservice
使用xfire+spring發布webservice其實很簡單,遵循一下幾個步驟即可
1. 想要發布成文webservice的類,必須實現接口
2. 3個配置文件(后面詳細說)
下面針對以上步驟詳細說明
關于1中的要求,給個例子就明白了
Itest.java 代碼
- package test;
-
- import org.json.JSONException;
-
- public interface IHello
- {
- public String hello();
- public String helloTo(String name);
- public String getJsonData(String pageIndex,String pageSize);
- }
HelloImpl.java 代碼
- package test;
-
- import java.util.*;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpSession;
-
- import org.codehaus.xfire.transport.http.XFireServletController;
- import org.json.JSONException;
- import org.json.JSONStringer;
-
- public class HelloImpl implements IHello
- {
-
- public String hello()
- {
- return "hello";
- }
-
- public String helloTo(String name)
- {
- return " hello " + name + "!";
- }
-
- private void example()
- {
-
- HttpServletRequest request = XFireServletController.getRequest();
- HttpSession session = request.getSession();
- }
-
- public String getJsonData(String pageIndex,String pageSize)
- {
- String rtnValue = "";
-
- rtnValue = "";
- for(int i=0;i<Integer.parseInt(pageSize);i++)
- {
- rtnValue = rtnValue + "{'lastname': 'last" + pageIndex + "-" + i + "', 'firstname': 'first" + pageIndex + "-" + i + "' },";
- }
- rtnValue = rtnValue + "{'lastname': 'last', 'firstname': 'last' }";
-
- rtnValue = "{'context':[" + rtnValue + "],'pager':[{'totalRecord':'100','totalpage':'10','pageIndex':'" + pageIndex + "','pageSize':'10'}]}";
-
- return rtnValue;
- }
- }
就這樣寫就可以了
關于三個配置文件
web.xml修改如下
xml 代碼
- <?xml version="1.0" encoding="UTF-8"?>
- <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">
-
- <context-param>
- <param-name>log4jConfigLocation</param-name>
- <param-value>/WEB-INF/classes/log4j.properties</param-value>
- </context-param>
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>
- /WEB-INF/classes/applicationContext*.xml
- classpath:org/codehaus/xfire/spring/xfire.xml
- </param-value>
- </context-param>
-
-
- <listener>
- <listener-class>
- org.springframework.web.context.ContextLoaderListener
- </listener-class>
- </listener>
- <listener>
- <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
- </listener>
-
- <listener>
- <listener-class>
- org.springframework.web.util.IntrospectorCleanupListener
- </listener-class>
- </listener>
-
-
- <servlet>
- <servlet-name>xfire</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- </servlet>
-
- <servlet-mapping>
- <servlet-name>xfire</servlet-name>
- <url-pattern>*.ws</url-pattern>
- </servlet-mapping>
-
- <welcome-file-list>
- <welcome-file>index.html</welcome-file>
- </welcome-file-list>
- </web-app>
這里注意 classpath:org/codehaus/xfire/spring/xfire.xml 必須要寫進去
xfire-servlet.xml 新建這個文件,并且和web.xml放在同一個文件夾
注意: 名稱和位置都不能變(也許可以改,我不知道)
xml 代碼
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
- <beans>
- <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
- <property name="urlMap">
- <map>
- <entry key="/testService.ws">
- <ref bean="test"/>
- </entry>
- </map>
- </property>
- </bean>
-
- <bean id="test" parent="webService" class="org.codehaus.xfire.spring.remoting.XFireExporter">
- <property name="serviceBean">
- <ref bean="testBean"/>
- </property>
- <property name="serviceClass">
- <value>test.IHello</value>
- </property>
- </bean>
-
-
- <bean id="webService" class="org.codehaus.xfire.spring.remoting.XFireExporter" abstract="true">
- <property name="serviceFactory">
- <ref bean="xfire.serviceFactory" />
- </property>
- <property name="xfire">
- <ref bean="xfire" />
- </property>
- </bean>
- </beans>
spring 的配置文件 applicationContext-webService.xml
xml 代碼
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
- <beans>
- <bean id="testBean" class="test.HelloImpl"></bean>
- </beans>
查看wsdl http://localhost:8080/mootools/
testService.ws?wsdl
運行 進入 index.html 頁面,點擊 即可執行調用,正常顯示表示發布成功,調用失敗會返回error(運行前請修改index.html文件的源碼,將url修改成真正的url)
例子很簡單,不過多解釋,源碼查看附件