XFire初学注意点

1.xfire默认会到src/META-INF/xfire目录下面寻找services.xml文件(

注意:这个META-INF目录并不是WebRoot下面的那个META-INF目录,而是src目录下面的)

2.IHelloService.aegis.xml文件存放位置,必须与IHelloService在同一个目录下面

3.服务接受的参数如果是List或Map,那么就是必须在IHelloService.aegis.xml中配置。如果是自定义数据类型,则没必要配置

<?xml version="1.0" encoding="UTF-8"?>
<mappings>
    <mapping>
        <method name="test">
            <parameter index="0" componentType="java.lang.String" /><!--传入的是List<String>-->
            <return-type componentType="test.Course" /><!--返回的结果是List<Course>-->
        </method>
   </mapping>
</mappings>

4.在本地测试服务时,注意引入两个可选的jar:commons-codec.jar和commons-httpclient.jar

import java.net.MalformedURLException;
import java.util.ArrayList;
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;

public class Client {

   public static void main(String[] args) {

	   /**
	    这部分概述如何使用XFire proxy classes.重复使用已存在的服务类和调用你的服务将是容易的。
            如果你需要调用别人的写的服务, 请查看 Client and Server Stub Generation from WSDL and Dynamic Client 部分。
           如果你是使用Spring,请查看相关的客户端信息在Spring Remoting部分
	    */
       //首先,你需要创建一个服务,它提供元数据关于服务是如何被组织的
       Service srvcModel = new ObjectServiceFactory().create(IHelloService.class);
       
       String helloWorldURL = "http://localhost:8089/XfireDemo/services/HelloService";
       
       try {
    	 //接下来,你需要为你的服务创建一个代理
    	   IHelloService srvc = (IHelloService)new XFireProxyFactory().create(srvcModel, helloWorldURL );
           System.out.println(srvc.sayHello("Robin"));
              
           User u=new User();
           u.setName("宇智波鼬");
           Course c=srvc.choose(u);
           System.out.println(c.getName());
           
           List  al=new ArrayList();
           al.add("彭传志");
           al.add("高红成");
           al.add("小萝莉");
           
           List t=srvc.test(al);
           
           for (int i = 0; i < t.size(); i++) {
              Course co=(Course)t.get(i);
              System.out.println(co.getName());
           }
     
       } catch (MalformedURLException e) {
           e.printStackTrace();
       }

   }

}

参考:

猜你喜欢

转载自weigang-gao.iteye.com/blog/2153070