最近用到了webservice,总结一下,主要是以下两点。
一.java的webservice client调用 C#的webservice
二.C#的webservice client调用 java的webservice
下面详细的说明一下,主要是java的client和service的说明,C#不熟。
一.java的webservice client调用 C#的webservice
1.写一个java的webservice client调用的通用类,代码如下:
package com.gdmes.core.utils.webservice; import org.apache.cxf.endpoint.dynamic.DynamicClientFactory; import org.apache.cxf.endpoint.Client; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * @Description: WebService客户端通用类 * @author zhaochuuhua * */ public class WebServiceClientHelper { public static Logger logger = LoggerFactory.getLogger("WebServiceClientHelper"); /** * @Title: callService * @Description: 调用远程的webservice并返回数据 * @param wsUrl * ws地址 * @param method * 调用的ws方法名 * @param arg * 参数 * @return:Object * @throws */ public static Object callService(String wsUrl, String method, Object[] param) { DynamicClientFactory dcf = DynamicClientFactory.newInstance (); Client client = dcf.createClient( wsUrl ); Object res = null; try { res = client.invoke( method, param ); } catch (Exception e) { logger.error("调用远程的webservice异常-"+e.getMessage()); } return res; } }
2.C# webservice返回的是List<数据对象>的数据格式,可以通过MyEclipse工具生成要用到数据对象,如下图:
之后,可以看一下是怎么调用的了、代码如下:
/** * ********************** * @param stationcode ******* * @param date yyyy-mm-dd * @return */ public String syncMarketDataClientMethod(String stationcode, String date){ String ipAdress = PropertyUtils.getPropertyValue(ConstantQuantity.getAPP_CONF_FILE(), IP_ADRESS); String wsUrl = "http://"+ipAdress+"/test/test.asmx?wsdl"; String method = "syncMarketData"; Object reply = (Object[])WebServiceClientHelper.callService(wsUrl, method, new Object[]{stationcode, date}); Object[] obj = (Object[])reply; if(obj != null){ ArrayOfMarketData arrayMarketData = (ArrayOfMarketData) obj[0]; ArrayList<MarketData> list = (ArrayList<MarketData>)arrayMarketData.getMarketData(); if(list != null && list.size() > 0){ //重复生成数据的校验 if(this.isExistTodayOfDataAtRundataTbl(stationcode, ConstantQuantity.getDAILYMARKETKIND(), date) == EXIST){ //该天的数据已经生成过了,可直接查询 return "exist"; }else{ //根据配置表的信息生成数据 return this.insertMarketData(stationcode, list); } }else{ return "empty"; } }else{ return "empty"; } }