实现axis2两种不同方式的客户端

来源:互联网 发布:java mac 绝对路径 编辑:程序博客网 时间:2024/05/17 08:47

1.一个简单的服务类 ,并把它发布为web service服务。注意我的工程名为Test

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


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

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;   
    }  
  
}  



注意上面实现document方式调用的时候,只测试了服务类中的add方法。这个方法都用了,each方法调用的写法就可知比这个还要简单。

http 请求方式调webservice

URLConnection theConnection = url.openConnection();
   connection = (HttpURLConnection)theConnection;
   connection.setRequestMethod("POST");
   connection.setConnectTimeout(7000);
   connection.setRequestProperty("Content-type", "text/xml;charset=UTF-8");
   connection.setRequestProperty("SOAPAction", "\"urn:SimpleInOutMessageReceiver\"");
   connection.setDoOutput(true);
   connection.setDoInput(true);
   connection.setUseCaches(false);
   connection.connect();

   String request = "<?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"><soapenv:Body>
       "</soapenv:Body>" + 
       "</soapenv:Envelope>";
   

   connection.getOutputStream().write(request.getBytes("UTF-8"));



原创粉丝点击