使用XFire调用Web服务 测式 案例!

1,在web.xml里配制

	
<!--配制XFireServlet  -->
<servlet>
<servlet-name>XFireServlet</servlet-name>
<servlet-class>
org.codehaus.xfire.transport.http.XFireConfigurableServlet
</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>XFireServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>

2,在service.xml里配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
<service>
<name>BizAccountService</name>
<serviceClass>cn.jbit.jboa.ws.BizAccountService</serviceClass>
<implementationClass>cn.jbit.jboa.ws.BizAccountServiceImpl
</implementationClass>
<style>wrapped</style>
<use>literal</use>
<scope>application</scope>
</service>
</beans>


3,创建包结构,如图


实现类代码如下:

package cn.jbit.jboa.ws;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.codehaus.xfire.transport.http.XFireConfigurableServlet;

import sun.org.mozilla.javascript.internal.Synchronizer;
import cn.jbit.jbos.bean.Account;

public class BizAccountServiceImpl implements BizAccountService {

	public List<Account> getBizAccount(String str) {
		System.out.println(str.trim());
		List<Account> list=new ArrayList<Account>();	
		Account a1=new Account();
		a1.setId(1);
		a1.setAcc_money(500);
		a1.setAcc_time(new Date());
		a1.setAcc_type('0');
		a1.setSheet_id(11);
		a1.setUsr_id(110);
		
		Account a2=new Account();
		a2.setId(2);
		a2.setAcc_money(1500);
		a2.setAcc_time(new Date());
		a2.setAcc_type('0');
		a2.setSheet_id(11);
		a2.setUsr_id(110);
		
		Account a3=new Account();
		a3.setId(3);
		a3.setAcc_money(3500);
		a3.setAcc_time(new Date());
		a3.setAcc_type('0');
		a3.setSheet_id(11);
		a3.setUsr_id(110);
		
		list.add(a1);
		list.add(a2);
		list.add(a3);
		
		System.out.println(list.size()+"  返回了");
		
		return list;
	}

	public  String testHello() {
		System.out.println("hello! testHello--------------");
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "world";
	}
	

}

4,测式类如下:

package cn.jbjt.jboa.ws.client;
import java.util.List;

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 cn.jbit.jboa.ws.BizAccountService;
import cn.jbit.jbos.bean.Account;

public class BizAccountSClient {

	/**
	 * @param args
	 * @throws InterruptedException 
	 */
	public static void main(String[] args)  {
		//创建服务接口模版
		Service serviceModel=new ObjectServiceFactory().
				create(BizAccountService.class);
		
		//创建代理工厂
		XFireProxyFactory factory=
				new XFireProxyFactory(XFireFactory.newInstance().getXFire());
        //定义web服务地址
		String accuntServicURL="http://localhost:8080/LedgerWebService/" +
				"services/BizAccountService";
		try {
			//根据接口模板和Web服务地址返回服务类
			BizAccountService bizAccount=
					(BizAccountService) factory.
					create(serviceModel,accuntServicURL);
		//根据参数调用方法,并反回结果
		
	   //测试反回字符串
	   String str=bizAccount.testHello();
	   System.out.println(str+"------------");
	   System.out.println("------------");
	
	 //测试反回字集合
		List<Account> list=bizAccount.getBizAccount("hello");
			if(list!=null){
			Account acc=null;
			for(int i=0;i<list.size();i++){
			acc=(Account) list.get(i);
			System.out.println(acc.getId()+" : "+acc.getAcc_money());
				}
			}
			
		} catch (Exception e) {
			// TODO: handle exception
		}
				
	}

}

OK!

需要注意可能会报错!原因是要替换一个jar包 wstx-asl-3.2.0.jar 换成wstx-asl-3.2.3.jar

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值