`
tw5566
  • 浏览: 449750 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

实现axis2两种不同方式的客户端(转载)

阅读更多
原文:http://johntor.iteye.com/blog/253605 
正在学习axis2,上面的文章对我们初学者来说是一个很大的帮助,便转了过来,非常感谢那些分享自己经验的人。 

1.一个简单的服务类 ,并把它发布为web service服务。注意我的工程名为Test 
Java代码 
package test;      
     
public class TestService {      
          
    public int add(int a, int b) {      
        return a + b;      
    }      
          
    public String each(String name) {      
        return name  + "你好";      
    }      
     
}    

package test;   
  
public class TestService {   
       
    public int add(int a, int b) {   
        return a + b;   
    }   
       
    public String each(String name) {   
        return name  + "你好";   
    }   
  
}  

2.应用两种方式实现客户端调用 

Java代码 
import javax.xml.namespace.QName;      
     
import org.apache.axiom.om.OMAbstractFactory;      
import org.apache.axiom.om.OMElement;      
import org.apache.axiom.om.OMFactory;      
import org.apache.axiom.om.OMNamespace;      
import org.apache.axis2.AxisFault;      
import org.apache.axis2.addressing.EndpointReference;      
import org.apache.axis2.client.Options;      
import org.apache.axis2.client.ServiceClient;      
import org.apache.axis2.rpc.client.RPCServiceClient;      
     
/**    
 * axis2提供rpc和document两种style的实现。    
 * 在这里我们别对其验证。关于说明,请参看代码中的注释    
 * @author Administrator    
 *    
 */     
public class Client {      
          
    public static void main(String[] args) {      
              
        Client client = new Client();      
        //测试rpc方式      
        client.testRPC();      
              
        //测试document方式      
        client.testDocument();      
    }      
          
    /**    
     * 应用rpc的方式调用    
     * 这种方式就等于远程调用,即通过url定位告诉远程服务器,告知方法名称,参数等,    
     * 调用远程服务,得到结果。    
     */     
    //下面这个annotaion是为了不让代码出现关于没有应用泛型的警告信息      
    //用elipse做编辑器的很容易理解。      
    @SuppressWarnings("unchecked")      
    public void testRPC() {      
        try {      
            RPCServiceClient serviceClient = new RPCServiceClient();      
            Options options = serviceClient.getOptions();      
            //指定访问的web service地址      
            EndpointReference targetEPR = new EndpointReference(      
                    "http://localhost:8080/Test/services/TestService");      
            options.setTo(targetEPR);      
     
            //指定方法,注意指定命名空间      
            QName opPrint = new QName("http://test",      
                    "add");      
            //确定参数类型和参数值      
            Class[] returnTypes = new Class[] { int.class };      
            Object obj[] = new Object[] { 1, 2 };      
            //得到返回结果,是一个数组      
            Object[] order = serviceClient.invokeBlocking(opPrint, obj,      
                    returnTypes);      
            System.out.println(order[0]);      
                  
                  
            //下面是测试each方法的。和上面类似      
            opPrint = new QName("http://test","each");      
            returnTypes = new Class[] { String.class };      
            obj = new Object[] { "zhangyt" };      
            order = serviceClient.invokeBlocking(opPrint, obj,      
                    returnTypes);      
            System.out.println(order[0]);      
                  
        } catch (AxisFault e) {      
            e.printStackTrace();      
        }         
    }      
          
    /**    
     * 应用document方式调用    
     * 用ducument方式应用现对繁琐而灵活。现在用的比较多。因为真正摆脱了我们不想要的耦合    
     */     
    public void testDocument() {      
        try {      
            ServiceClient sc = new ServiceClient();      
            Options opts = new Options();      
            //确定目标服务地址      
            opts.setTo(new EndpointReference(      
                    "http://localhost:8080/Test/services/TestService"));      
            //确定调用方法      
            opts.setAction("urn:add");      
            sc.setOptions(opts);      
            //发送请求并并得到返回结果,注意参数生成方法的分析      
            OMElement res = sc.sendReceive(createPayLoad());      
            //值得注意的是,返回结果就是一段由OMElement对象封装的xml字符串。      
            //我们可以对之灵活应用,下面我取第一个元素值,并打印之。因为调用的方法返回一个结果      
            res.getFirstElement().getText();      
            System.out.println(res.getFirstElement().getText());      
        } catch (AxisFault e) {      
            e.printStackTrace();      
        }      
    }      
    /**    
     * ServiceClient 中 sendReceive(OMElement args) 方法参数的生成    
     * @return    
     */     
    public static OMElement createPayLoad(){       
        OMFactory fac = OMAbstractFactory.getOMFactory();      
        //指定命名空间      
        OMNamespace omNs = fac.createOMNamespace("http://test", "nsl");      
        //指定方法      
        OMElement method = fac.createOMElement("add",omNs);      
        //指定方法的参数      
        OMElement value = fac.createOMElement("value",omNs);       
        value.setText("1");       
        method.addChild(value);       
        OMElement value1 = fac.createOMElement("value",omNs);      
        value1.setText("2");      
        method.addChild(value1);      
        //返回方法(实际上就是OMElement封装的xml字符串)      
        return method;       
    }      
     
}   

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics