调用axis2 WebService三种方法(转载)

 第一:简单的使用axis2包自己实现调用

import javax.xml.namespace.QName;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;

public class WebServiceUtils
{
 // ----------------------------------------------------- Properties
 public static int TIMEOUT = 100000;

 // ----------------------------------------------------- Constructors

 // ----------------------------------------------------- Methods
 /**
  * 
  * 调用axis2的webservice的方法
  * 
  * @return
  */
 @SuppressWarnings("unchecked")
 static public Object[] invokeWebService( String url, String nameSpace, String method, Object[] args, Class[] returnTypes )
   throws AxisFault
 {
  RPCServiceClient serviceClient = new RPCServiceClient();
  Options options = serviceClient.getOptions();
  EndpointReference targetEPR = new EndpointReference( url );
  options.setTo( targetEPR );
  options.setTimeOutInMilliSeconds( TIMEOUT );
  QName opName = new QName( nameSpace, method );
  Object[] results = serviceClient.invokeBlocking( opName, args, returnTypes );
  return results;
 }
}

 

第二、

 

public static OMElement getEchoOMElement() {         
    OMFactory fac = OMAbstractFactory.getOMFactory();         
    OMNamespace omNs = fac.createOMNamespace(                 "http://example1.org/example1", "example1");         
    OMElement method = fac.createOMElement("echo", omNs);         
    OMElement value = fac.createOMElement("Text", omNs);         
    value.addChild(fac.createOMText(value, "Axis2 Echo String "));    
    method.addChild(value);
     return method;     
}

public static void main(String[] args) {
    try {            
        OMElement payload = ClientUtil.getEchoOMElement();             
        Options options = new Options();                        
        options.setTo(targetEPR);             
        options.setAction("urn:echo");
        //Blocking invocation             
        ServiceClient sender = new ServiceClient();             
        sender.setOptions(options);             
        OMElement result = sender.sendReceive(payload);
        System.out.println(result);                        
        System.out.println(options.getTo());
    } catch (AxisFault axisFault) {             
        axisFault.printStackTrace();         
    }     
}

 

第三、利用插件生成客户端

private static String URL="http://localhost:8080/axis2/services/WeatherService";  
    public static void main(String[] args) throws RemoteException {
        try {
            WeatherServiceStub stub;
            if (args != null && args.length != 0) {
                stub = new WeatherServiceStub(args[0]);
               
            } else {
                stub = new WeatherServiceStub(URL);
            }
            WeatherServiceStub.SetWeather setWether = new WeatherServiceStub.SetWeather();
            WeatherServiceStub.Weather wether = new WeatherServiceStub.Weather();
           
            wether.setForecast("east");
            wether.setRain(true);
            wether.setTemperature(12.3f);
            wether.setForecast("big");
           
            setWether.setWeather(wether);
            stub.setWeather(setWether);
           
            WeatherServiceStub.GetWeatherResponse gw = new WeatherServiceStub.GetWeatherResponse();
            System.out.println(gw.get_return());        
           
        } catch (AxisFault e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

 

 

 

本文部分来自CSDN博客,转载请标明出处:http://blog.csdn.net/yhhah/archive/2009/05/07/4158487.aspx

posted @ 2012-12-29 14:16  阳光下的小丑  阅读(293)  评论(0)    收藏  举报