欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Axis、Axis2、CXF调用WebService的几种方式

程序员文章站 2022-06-11 08:21:36
...
第一种:
Axis调用webService:

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.message.SOAPHeaderElement;
(注意这里引用的包不是axis2是axis)

public static String ask(String url,String method,Object[] params){
String result = null;
try {
            Service service = new Service();

            Call call = (Call) service.createCall();
            call.setTargetEndpointAddress(new java.net.URL(url));
        
            call.setProperty(Call.CONNECTION_TIMEOUT_PROPERTY, new Integer(
60 * 60000));
call.setTimeout(new Integer(60 * 60000));
            call.setOperationName(method);//WSDL里面描述的接口名称

            result = (String)call.invoke(params);

} catch (Exception e) {
log.error("调用webservice接口异常");
e.printStackTrace();
}
return result;
}


第二种:
axis2调用webService
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的包)

    public void testRPC(){
        try{
            RPCServiceClient client = new RPCServiceClient();
            Options options = client.getOptions();
//            String url = "http://localhost:8080/axis2/services/HelloWebService?wsdl";
            String url = "http://localhost:8089/webserviceServer/TestService?wsdl";
            EndpointReference end = new EndpointReference(url);
            options.setTo(end);
            Object[] obj = new Object[]{"tom"};
             Class<?>[] classes = new Class[] { String.class }; 
            QName qname = new QName("http://testClick", "sayHello");
            String result = (String) client.invokeBlocking(qname, obj,classes)[0];
            System.out.println(result);
        }catch(AxisFault e){
            e.printStackTrace();
        }
    }

第三种:

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;
(引入的包同第二种一样)
    public void testOMElement(){
        try{
            ServiceClient sc = new ServiceClient();
            Options opts = new Options();
            String url = "http://localhost:8080/axis2/services/HelloWebService?wsdl";
            EndpointReference end = new EndpointReference(url);
            opts.setTo(end);
            opts.setAction("sayHello");
            sc.setOptions(opts);
            
            OMFactory fac = OMAbstractFactory.getOMFactory(); 
            OMNamespace omNs = fac.createOMNamespace("http://testClick", ""); 
            OMElement method = fac.createOMElement("sayHello",omNs); 
            OMElement value = fac.createOMElement("name",omNs);
            value.setText("admin");
            method.addChild(value);
            OMElement res = sc.sendReceive(method);
            res.getFirstElement().getText(); 
            System.out.println(res.getFirstElement().getText());
        }catch(AxisFault e){
            e.printStackTrace();
        }
    }


CXF调用webService:
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
String cvsUrl = "http://xxxxxx?wsdl";
Client client = dcf.createClient(cvsUrl);
String params = "{\"username\":\""+config.getDataAcc()+"\",\"password\":\""+config.getDataPwd()+"\",\"jobName\":\""+holeTask.getTaskName()+"\"}";
objects = client.invoke("generalAssetResult", params);

参考文档:
http://www.2cto.com/kf/201311/257446.html


相关标签: Axis2 Axis CXF