用XFire1.2来开发WebService

参考:

 http://kaguvivian.javaeye.com/blog/114670  Eclipse下使用Xfire创建WebService入门示例

http://www.360doc.com/showweb/0/0/516946.aspx  XFire:轻松简单地开发Web Services

我直接帖自己修改的代码了...(太懒了...反正也是参看别人的....)

先是一个WebService接口:  (IBankingService )
------------------------------------------------------------------------------------
package com.thx.bank;

public interface IBankingService {

 public String transferFunds(
   String fromAccount, String toAccount,double amount, String currency);
}
-----------------------------------------------------------------------------------

然后实现类: (BankService)
-----------------------------------------------------------------------------------
package com.thx.bank;

import java.text.DecimalFormat;
import java.text.NumberFormat;

public class BankService implements IBankingService {

 public BankService(){}
 
 @Override
 public String transferFunds(String fromAccount, String toAccount,
   double amount, String currency) {

  String msg = "";
  
  try{
   NumberFormat nf = new DecimalFormat("###,###,###,###.00");
   msg = "COMPLETED: " + currency + " " + nf.format(amount)+    
         " was successfully transferred from A/C# " + fromAccount + " to A/C# " + toAccount;
  }catch(Exception e){
   msg = e.toString();
  }
  return msg;
 }

}
----------------------------------------------------------------------------------

客户端测试: (BankClient)
--------------------------------------------------------------
package com.thx.bank.client;

import java.net.MalformedURLException;
import org.codehaus.xfire.XFire;
import org.codehaus.xfire.XFireFactory;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;

import com.thx.bank.IBankingService;

public class BankClient {

 public String callWebService(String fromAccount, String toAccount,double amount, String currency){
  //Create a metadata of the service 创建一个service的元数据service
  Service serviceModel = new ObjectServiceFactory().create(IBankingService.class);
  System.out.println("callSoapServiceLocal(): got service model." );
  //Create a proxy for the deployed service 用XFireProxyFactory(代理工厂)为发布的服务创建一个代理工厂
  XFire xfire = XFireFactory.newInstance().getXFire();
  XFireProxyFactory factory = new XFireProxyFactory(xfire);
  //得到一个服务的本地代理
  //String serviceUrl = http://localhost:8080/WSDemo/services/Banking;
  String serviceUrl = "xfire.local://Banking";//换成这个访问速度更快(资料上说的),不管怎么说至少没hardcode了
  IBankingService client = null;
  //代理工厂创建客户端服务对象
  try{
   client = (IBankingService)factory.create(serviceModel,serviceUrl);
  }catch(MalformedURLException e){
   System.out.println("WsClient.callWebService(): EXCEPTION: " + e.toString());
  }
  //调用服务 返回状态结果信息
  String serviceResponse = "";
  try {
   serviceResponse = client.transferFunds(fromAccount, toAccount, amount, currency);
  } catch (Exception e) {
   System.out.println("WsClient.callWebService(): EXCEPTION: " + e.toString());
   serviceResponse = e.toString();
  }
  return serviceResponse;
 }
}

----------------------------------------------------------------

用MyEclipse创建的WebService服务会在左边的项目的结构中自动创建一个和WebRoot同级的WebServices目录,里面有一个services.xml(注意是复数),该文件在项目部署后在(项目名/WEB-INF/classes/META-INF)目录下
---------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">

 <service>
  <name>Banking</name>
  <namespace>mybank</namespace>
  <serviceClass>com.thx.bank.IBankingService</serviceClass>
  <implementationClass>com.thx.bank.BankService</implementationClass>
 </service>

</beans>
--------------------------------------------------------------------------------------

测试页面: (index.jsp)
-------------------------------------------------
<%@ page contentType="text/html; charset=utf-8"%>
<html>
  <body>
    Bank test : give my money to you!   
     <form name="form1" action="say.jsp" method="POST">   
      your account ID:<input name="toAcc" type="text"/><br/>
      How much do you want:<input name="amount" type="text" />
      <input type="submit" value="提交">
      </form>
  </body>
</html>
------------------------------------------------------

调用WebService页(say.jsp)
------------------------------------------------------
<%@ page contentType="text/html; charset=utf-8"%>
<%@ page import="com.thx.bank.client.BankClient"%>
<%
 String toAcc = request.getParameter("toAcc");
 double amount = new Integer(request.getParameter("amount")).doubleValue();
 String fromAccount = "sophie";
 String currency = "RMB$";
 String Message = new BankClient().callWebService(fromAccount,toAcc,amount,currency);
%>
<html>
  <body>
    Message from WebService:<%=Message %>
  </body>
</html>
-----------------------------------------------------

请注意一下自动生成的web.xml,功能其实就是过滤器.

好了,祝测试成功.

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值