老兄,快来一起过春天!(八)

Spring集成Xfire

 

用XFire 打造webServices

 

一:下载XFire1.26

官网地址:http://xfire.codehaus.org/Download
下载 xfire-distribution-1.2.6.zip 与 xfire-all-1.2.6.jar 包,如单独下载jar包,会发生譬如找不到Spring依赖或Ant依赖等少包问题,因此最好把依赖jar包与核心jar包都下了,甩到lib中,万事无忧。

 

二:Xfire 服务端  VS  Xfire客户端
(一):Xfire 服务端构建

1:新建web工程 TestXfire ,将刚下载到的包括核心与依赖jar都丢到web-inf/lib中作为外部jar包引用。

2:打开xml,加入以下代码,将Xfire解析加到web工程中,在init-param的value属性中,规定了Xfire在哪里查找xfire配置文件,可根据自己需要更改文件位置。

  1. ..........
  2.     <servlet>
  3.          <servlet-name>XFireServlet</servlet-name>
  4.          <servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>
  5.          <init-param>
  6.             <param-name>config</param-name>
  7.             <param-value>xfire/services.xml</param-value>
  8.          </init-param>
  9.     </servlet>
  10.      <servlet-mapping>
  11.          <servlet-name>XFireServlet</servlet-name>
  12.          <url-pattern>/servlet/XFireServlet/*</url-pattern>
  13.      </servlet-mapping>
  14.      <servlet-mapping>
  15.          <servlet-name>XFireServlet</servlet-name>
  16.          <url-pattern>/services/*</url-pattern>
  17.      </servlet-mapping>
  18. ..........

3:新建包:xfire,在之下新建 services.xml ,作为 xfire 向外部暴露的web服务配置文件。

4:

新建包:com.testxfire.dao ,之下新建web服务接口:ICalculation.java ,接口中声明了用于向外部暴露的计算服务方法:

  1. package com.testxfire.dao;
  2. public interface ICalculation {
  3.     
  4.     public String CalculationMain(int type,double p1,double p2);
  5. }

新建包:com.testxfire.dao.impl ,之下ICalculation实现类:CalculationImpl.java

  1. package com.testxfire.dao.impl;
  2. import java.text.DecimalFormat;
  3. import com.testxfire.dao.ICalculation;
  4. public class CalculationImpl implements ICalculation {
  5.     public String CalculationMain(int type, double p1, double p2) {
  6.         String str = "";
  7.         
  8.         try{
  9.             DecimalFormat df=new DecimalFormat("#.00"); 
  10.             
  11.             switch (type) {
  12.             case 1:
  13.                 str = df.format(p1 + p2);
  14.                 break;
  15.             case 2:
  16.                 str = df.format(p1 - p2);
  17.                 break;
  18.             case 3:
  19.                 str = df.format(p1 * p2);
  20.                 break;
  21.             case 4:
  22.                 str = df.format(p1 / p2);
  23.                 break;
  24.             default:
  25.                 str = "无效的运算符";
  26.                 break;
  27.             }
  28.         }catch(Exception es){
  29.             str = "运算发生未知错误";
  30.         }
  31.         
  32.         return str;
  33.     }
  34. }

5:因为第4步新建了用于web服务的接口及实现类,因此需要在xfire的配置文件 services.xml 中声明它们,这样Xfire可通过读取配置文件得到需要向外暴露的接口:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://xfire.codehaus.org/config/1.0">
  3.     <service>
  4.         <name>calculation</name>
  5.         <namespace>ws_cal</namespace>
  6.         <serviceClass>com.testxfire.dao.ICalculation</serviceClass>
  7.         <implementationClass>com.testxfire.dao.impl.CalculationImpl</implementationClass>
  8.     </service>
  9. </beans>

6:整个过程完成后,部署到Tomcat运行,输入 http://localhost:7999/TestXfire/services/calculation?wsdl ,如可以看到浏览器找到了解释此 webServices 系统的wsdl文件,说明服务端构建成功。

 

(二):Xfire 客户端构建

1:新建web工程TestXfireClient,同样把下载的xfire包丢到lib中引用。

2:新建包:com.clientbean ,之下新建 Servlet类:WebClientBean.java

  1. package com.clientbean;
  2. import java.io.IOException;
  3. import java.net.URL;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import org.codehaus.xfire.client.Client;
  9. public class WebClientBean extends HttpServlet {
  10.     protected void doGet(HttpServletRequest req, HttpServletResponse resp){
  11.         try {           
  12.             doPost(req, resp);
  13.         } catch (Exception e) {
  14.             e.printStackTrace();
  15.         }
  16.     }
  17.     protected void doPost(HttpServletRequest req, HttpServletResponse resp)
  18.             throws ServletException, IOException {      
  19.         String p1 = req.getParameter("p1");
  20.         double p = Double.parseDouble(p1);
  21.         
  22.         Client client;
  23.         try {
  24.             client = new Client(new URL("http://localhost:7999/TestXfire/services/calculation?wsdl"));
  25.             Object[] results = client.invoke("CalculationMain"new Object[] {new Integer(1), p,p});
  26.             System.out.println((String) results[0]);        
  27.             System.out.println(p1);
  28.         } catch (Exception e) {
  29.             e.printStackTrace();
  30.         }
  31.     }   
  32. }

3:将WebClientBean.java 在web.xml 中声明为servlet。

  1.   <servlet>
  2.     <servlet-name>wsclient</servlet-name>
  3.     <servlet-class>com.clientbean.WebClientBean</servlet-class>
  4.   </servlet>
  5.   
  6.   <servlet-mapping>
  7.     <servlet-name>wsclient</servlet-name>
  8.     <url-pattern>/wsclient</url-pattern>
  9.   </servlet-mapping>

4:在首页 index.jsp 中建立一个表单提交,提交到WebClientBean

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  3. <html>
  4.   <head>    
  5.     <title>调用远程webservice</title>
  6.     <meta http-equiv="pragma" content="no-cache">
  7.     <meta http-equiv="cache-control" content="no-cache">
  8.     <meta http-equiv="expires" content="0">    
  9.   </head>
  10.   
  11.   <body>
  12.     <form action="/TestXfireClient/wsclient">
  13.         <input type="hidden" id="p1" name="p1" value="4.5" />
  14.         <input type="submit" value="按下调用webServices计算 4.5 + 4.5" />
  15.     </form>
  16.   </body>
  17. </html>

可以看到在WebClientBean.java中,XFire调用远程web服务需要在获得WSDL描述文件后,必须设定一个远程代理(代理需传入调用的方法名、方法参数)。XFire 只用了2句代码就调用了一个远程web服务并返回结果,当然这份代码前提是web服务没有进行加密。因此XFire相比于其他webservice如ASIX,有着很大的便捷性,更重要XFire是新一代的WebService,是基于SOAP的。部署到Tomcat运行后,按下index.jsp的按钮,转到eclipse的控制台,可以看到 WebClientBean.java 中 System.out.println((String) results[0]);  这句代码打印出调用后的结果:9.0 。

 

Spring 集成 Xfire

 

1:新建web工程TestSpringXfire,加入下载的Xfire包,然后从中删除掉Spring1.26 jar包,以免与后面的Spring2.0发生冲突,另外加入Spring2.0 core、web包。

2:在web.xml中加入Spring、Xfire解析。

Spring:

  1. ..........
  2.   <context-param>
  3.     <param-name>contextConfigLocation</param-name>
  4.     <param-value>classpath:org/codehaus/xfire/spring/xfire.xml,/WEB-INF/ApplicationContext.xml</param-value>
  5.   </context-param>
  6.   <listener>
  7.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  8.   </listener>
  9. ..........

Xfire:

  1. ...........
  2.   <servlet>
  3.     <servlet-name>XFireServlet</servlet-name>
  4.     <servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class>
  5.   </servlet>
  6.   <servlet-mapping>
  7.     <servlet-name>XFireServlet</servlet-name>
  8.     <url-pattern>/servlet/XFireServlet/*</url-pattern>
  9.   </servlet-mapping>
  10.   <servlet-mapping>
  11.     <servlet-name>XFireServlet</servlet-name>
  12.     <url-pattern>/services/*</url-pattern>
  13.   </servlet-mapping>
  14. ...........

3:将之前TestXfire新建的接口与其实现类粘贴到此工程中来

4:在web.xml中声明Spring配置文件的位置加入Spring配置文件,此工程配置文件名为:ApplicationContext.xml 。打开此xml,在<beans>节点中加入:

  1. ..........
  2.     <bean id="calWS" class=" com.testxfire.dao.impl.CalculationImpl"/>
  3.     <bean id="addressingHandler" class="org.codehaus.xfire.addressing.AddressingInHandler" />
  4.     
  5.     <bean name="wsService" class="org.codehaus.xfire.spring.ServiceBean" >
  6.         <property name="serviceBean" ref="calWS" ></property>
  7.         <property name="serviceClass" value="com.testxfire.dao.ICalculation"></property>
  8.         <property name="inHandlers">
  9.             <list>
  10.                 <ref bean="addressingHandler" />
  11.             </list>
  12.         </property>
  13.     </bean> 
  14. ..........

此段代码主要是将web服务暴露的外部接口交给了Spring去管理,并且由spring去决定是否开放此web服务。

5:部署到Tomcat运行,输入:http://localhost:7999/TestSpringXfire/services,可以web服务页面,上面标注了此web服务开放的服务接口。

6:第5步中标注的服务接口后有每个服务接口的wsdl链接,点击后将wsdl的url复制,替换掉TestXfireClient工程中WebClientBean.java中用到获取wsdl描述文件的wsdl url。运行TestXfireClient,可以看到后台打印出调用结果。

 

package com.xfire.core.client; import java.net.MalformedURLException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.codehaus.xfire.client.XFireProxyFactory; import org.codehaus.xfire.service.Service; import org.codehaus.xfire.service.binding.ObjectServiceFactory; import com.xfire.core.entity.UserInfo; import com.xfire.core.service.IUserInfoService; /** *@author jilongliang *@Date 2012-3-5 * */ public class UserInfoClient { public static void main(String[] args) { getServiceList(); setServiceList(); } static String url = "http://localhost:8081/xfire/services/UserInfo"; /** * */ public static void getServiceList() { Service service = new ObjectServiceFactory() .create(IUserInfoService.class); try { IUserInfoService iAddressService = (IUserInfoService) new XFireProxyFactory() .create(service, url); List list = (ArrayList) iAddressService .getAddressList(); System.out.println("一共多少条数据:" + list.size()); for (Iterator iter = list.iterator(); iter.hasNext();) { UserInfo a = iter.next(); System.out.println(a); } } catch (MalformedURLException e) { e.printStackTrace(); } } public static void setServiceList() { Service service = new ObjectServiceFactory() .create(IUserInfoService.class); try { IUserInfoService iAddressService = (IUserInfoService) new XFireProxyFactory() .create(service, url); List listAdd = new ArrayList(); UserInfo address = new UserInfo(); address.setIdentifier(1); address.setCountry("中國"); address.setProivice("廣東省"); address.setCity("陽江"); address.setAddress("廣東陽春"); address.setPostCode("1111111"); address.setExist(false); address.setArrary(new String[] { "22", "23", "24" }); listAdd.add(address); address.setIdentifier(2); address.setCountry("中國"); address.setProivice("廣東省"); address.setCity("陽江"); address.setAddress(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值