当前位置:首页 > 开发 > 编程语言 > Web Service > 正文

调用axis2 WebService三种方法

发表于: 2013-06-28   作者:coconut_zhang   来源:转载   浏览:
摘要: 第一:简单的使用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

第一:简单的使用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();
        }
    }

调用axis2 WebService三种方法

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号