1. 程式人生 > >通用的webService(CXF、Axis)呼叫工具類(無強制依賴)

通用的webService(CXF、Axis)呼叫工具類(無強制依賴)

要支援Axis需要這麼幾個不常見的依賴:

<dependency>
   <groupId>org.apache.axis</groupId>
	   <artifactId>axis</artifactId>
			<version>1.4</version>
		</dependency>
		<dependency>
			<groupId>javax.xml.rpc</groupId>
			<artifactId>javax.xml.rpc-api</artifactId>
			<version>1.1.1</version>
		</dependency>
		<dependency>
			<groupId>commons-discovery</groupId>
			<artifactId>commons-discovery</artifactId>
			<version>0.2</version>
		</dependency>
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 = JaxWsDynamicClientFactory.newInstance();
		Client client = dcf.createClient(wsdl);
		Object[] objects = client.invoke(operationName, paramXml);
		// 輸出呼叫結果
		if (objects == null || objects.length < 1) {
			return new ClientRes(false, "返回引數列表為空!");
		}
		return XMLUtil.analysisResXml(String.valueOf(objects[0]));
	}

	public static ClientRes invokeAxis(AxisEnum axisEnum, String paramXml) throws RemoteException, ServiceException {

		String result;
		String wsdl = axisEnum.getWsdl();
		String operationName = axisEnum.getOperationName();
		String paramName = axisEnum.getParamName();

		logger.info("--------------------準備呼叫介面");
		logger.info("-------傳送的wsdl:{}", wsdl);
		logger.info("-------傳送的operationName:{}", operationName);
		logger.info("-------傳送的paramName:{}", paramName);
		logger.info("-------傳送的paramXml:{}", paramXml);

		Service service = new Service();
		Call call = (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 = new Object[] { paramXml };
		// 給方法傳遞引數,並且呼叫方法
		result = (String) call.invoke(paramValues);
		if (StringUtils.isEmpty(result)) {
			return new ClientRes(false, "返回引數列表為空!");
		}
		return XMLUtil.analysisResXml(result);

	}

//	public static ClientRes invokeAxis(File file) {
//		System.out.println("開始呼叫WebService");
//		try {
//			String endpoint = "http://localhost:8080/MyProject/services/Document";
//			Service service = new Service();
//			Call call = (Call) service.createCall();
//			call.setTargetEndpointAddress(new java.net.URL(endpoint));
//			call.setOperationName(new QName(endpoint, "addAttachmentInfo"));
//			QName qnameattachment = new QName("urn:beanservice", "DataHandler");
//
//			DataHandler dh = new DataHandler(new FileDataSource(file));
//
//			call.registerTypeMapping(dh.getClass(), qnameattachment, JAFDataHandlerSerializerFactory.class,
//					JAFDataHandlerDeserializerFactory.class);
//
//			//String rev = (String) call.invoke(new Object[] { dh, "中國工產黨.doc" });
//			String rev = (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 = LoggerFactory.getLogger(ZJWebServiceClientUtil.class);

	private ZJWebServiceClientUtil() {
	}
}