qqjianyue代码工

砌java代码
posts - 62, comments - 9, trackbacks - 0, articles - 10
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

调用axis2 WebService三种方法

Posted on 2010-01-20 23:16 Qzi 阅读(10496) 评论(0)  编辑  收藏 所属分类: Web一般性问题
第一:简单的使用axis2包自己实现调用

package common;

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


只有注册用户登录后才能发表评论。


网站导航: