要支持Axis需要这么几个不常见的依赖:
org.apache.axisaxis1.4javax.xml.rpcjavax.xml.rpc-api1.1.1commons-discoverycommons-discovery0.2
import java.rmi.RemoteException;import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.commons.lang.StringUtils;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import com.bda.common.util.WebServiceUtil;
import com.bda.tag.zjdevice.api.cxf.bean.ClientRes;
import com.bda.tag.zjdevice.api.cxf.constant.AxisEnum;/*** * @author Dongguabai**/
public class ZJWebServiceClientUtil extends WebServiceUtil {public static ClientRes invokeCXF(String wsdl, String operationName, String paramXml) throws Exception {JaxWsDynamicClientFactory dcf &#61; JaxWsDynamicClientFactory.newInstance();Client client &#61; dcf.createClient(wsdl);Object[] objects &#61; client.invoke(operationName, paramXml);// 输出调用结果if (objects &#61;&#61; null || objects.length <1) {return new ClientRes(false, "返回参数列表为空&#xff01;");}return XMLUtil.analysisResXml(String.valueOf(objects[0]));}public static ClientRes invokeAxis(AxisEnum axisEnum, String paramXml) throws RemoteException, ServiceException {String result;String wsdl &#61; axisEnum.getWsdl();String operationName &#61; axisEnum.getOperationName();String paramName &#61; axisEnum.getParamName();logger.info("--------------------准备调用接口");logger.info("-------发送的wsdl:{}", wsdl);logger.info("-------发送的operationName:{}", operationName);logger.info("-------发送的paramName:{}", paramName);logger.info("-------发送的paramXml:{}", paramXml);Service service &#61; new Service();Call call &#61; (Call) service.createCall();call.setTargetEndpointAddress(wsdl);call.setEncodingStyle("utf-8");// WSDL里面描述的接口名称(要调用的方法)call.setOperationName(operationName);// 接口方法的参数名, 参数类型,参数模式 IN(输入), OUT(输出) or INOUT(输入输出)call.addParameter(paramName, XMLType.SOAP_STRING, ParameterMode.IN);// 设置被调用方法的返回值类型call.setReturnType(XMLType.XSD_STRING);// 设置方法中参数的值Object[] paramValues &#61; new Object[] { paramXml };// 给方法传递参数&#xff0c;并且调用方法result &#61; (String) call.invoke(paramValues);if (StringUtils.isEmpty(result)) {return new ClientRes(false, "返回参数列表为空&#xff01;");}return XMLUtil.analysisResXml(result);}// public static ClientRes invokeAxis(File file) {
// System.out.println("开始调用WebService");
// try {
// String endpoint &#61; "http://localhost:8080/MyProject/services/Document";
// Service service &#61; new Service();
// Call call &#61; (Call) service.createCall();
// call.setTargetEndpointAddress(new java.net.URL(endpoint));
// call.setOperationName(new QName(endpoint, "addAttachmentInfo"));
// QName qnameattachment &#61; new QName("urn:beanservice", "DataHandler");
//
// DataHandler dh &#61; new DataHandler(new FileDataSource(file));
//
// call.registerTypeMapping(dh.getClass(), qnameattachment, JAFDataHandlerSerializerFactory.class,
// JAFDataHandlerDeserializerFactory.class);
//
// //String rev &#61; (String) call.invoke(new Object[] { dh, "中国工产党.doc" });
// String rev &#61; (String) call.invoke(new Object[] {});
//
// System.out.println(rev);
// } catch (Exception e) {
// System.out.print(e.toString());
// }
//
// System.out.println("调用WebService正常结束");
// return null;
// }private static final Logger logger &#61; LoggerFactory.getLogger(ZJWebServiceClientUtil.class);private ZJWebServiceClientUtil() {}
}