利用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)
例子很简单,不过多解释,源码查看附件