使用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,创建包结构,如图

使用XFire调用Web服务 测式 案例!_第1张图片

实现类代码如下:

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

你可能感兴趣的:(xfire)