/**
*
* @param wsdlUrl wsdl的地址:http://localhost:8001/demo/HelloServiceDemoUrl?wsdl
* @param methodName 调用的方法名称 selectOrderInfo
* @param targetNamespace 命名空间 http://service.limp.com/
* @param name name HelloServiceDemo
* @param paramList 参数集合
* @throws Exception
*/
public static String dynamicCallWebServiceByCXF(String wsdlUrl,String methodName,String targetNamespace,String name,List<Object> paramList)throws Exception{
//临时增加缓存,增加创建速度
if(!factoryMap.containsKey(methodName)){
// 创建动态客户端
JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
// 创建客户端连接
Client client = factory.createClient(wsdlUrl);
ClientImpl clientImpl = (ClientImpl) client;
Endpoint endpoint = clientImpl.getEndpoint();
factoryMap.put(methodName,endpoint);
clientMap.put(methodName,client);
System.out.println("初始化");
}
//从缓存中换取 endpoint、client
Endpoint endpoint=factoryMap.get(methodName);
Client client=clientMap.get(methodName);
// Make use of CXF service model to introspect the existing WSDL
ServiceInfo serviceInfo = endpoint.getService().getServiceInfos().get(0);
// 创建QName来指定NameSpace和要调用的service
String localPart=name+"SoapBinding";
QName bindingName = new QName(targetNamespace, localPart);
BindingInfo binding = serviceInfo.getBinding(bindingName);
//创建QName来指定NameSpace和要调用的方法绑定方法
QName opName = new QName(targetNamespace, methodName);//selectOrderInfo
BindingOperationInfo boi = binding.getOperation(opName);
// BindingMessageInfo inputMessageInfo = boi.getInput();
BindingMessageInfo inputMessageInfo = null;
if (!boi.isUnwrapped()) {
//OrderProcess uses document literal wrapped style.
inputMessageInfo = boi.getWrappedOperation().getInput();
} else {
inputMessageInfo = boi.getUnwrappedOperation().getInput();
}
List<MessagePartInfo> parts = inputMessageInfo.getMessageParts();
/***********************以下是初始化参数,组装参数;处理返回结果的过程******************************************/
Object[] parameters = new Object[parts.size()];
for(int m=0;m<parts.size();m++){
MessagePartInfo part=parts.get(m);
// 取得对象实例
Class<?> partClass = part.getTypeClass();//OrderInfo.class;
System.out.println(partClass.getCanonicalName()); // GetAgentDetails
//实例化对象
Object initDomain=null;
//普通参数的形参,不需要fastJson转换直接赋值即可
if("java.lang.String".equalsIgnoreCase(partClass.getCanonicalName())
||"int".equalsIgnoreCase(partClass.getCanonicalName())){
initDomain=paramList.get(m).toString();
}
//如果是数组
else if(partClass.getCanonicalName().indexOf("[]")>-1){
//转换数组
initDomain=JSON.parseArray(paramList.get(m).toString(),partClass.getComponentType());
}else{
initDomain=JSON.parseObject(paramList.get(m).toString(),partClass);
}
parameters[m]=initDomain;
}
//定义返回结果集
Object[] result=null;
//普通参数情况 || 对象参数情况 1个参数 ||ArryList集合
try {
result = client.invoke(opName,parameters);
}catch (Exception ex){
ex.printStackTrace();
return "参数异常"+ex.getMessage();
}
//返回调用结果
if(result.length>0){
return JSON.toJSON(result[0]).toString();
}
return "invoke success, but is void ";
}
改进分析:
1.调用参数初始化和返回结果初始化 利用fastjson快速实体化
2.借用缓存可以完成快速创建的效果(日后可以redis存放缓存结果,不知道有木有坑,可以共同讨论哈)
3.完全支持List集合、JavaBean、 多个(多个多个)参数.
public static void main(String[] args) throws Exception{
/*********************参数初始化过程************************************/
String str="[{\"id\":\"NO.1\",\"money\":23},{\"id\":\"NO.2\",\"money\":24}]";
Object initDomain=JSON.parseArray(str,OrderInfo.class);
// pojoInvokes1();
List<Object> listParam=new ArrayList<>();
String params="{\"id\":\"zhangsan\",\"money\":23}";
listParam.add(params);
//////
List<Object> listParam2=new ArrayList<>();
String obj0="超级管理员";
String obj1="{\"id\":\"zhangsan\",\"money\":23}";
String obj2="{\"name\":\"one test\",\"intro\":\"这是订单详情\"}";
listParam2.add(obj0);
listParam2.add(obj1);
listParam2.add(obj2);
/////
List<Object> listParam1=new ArrayList<>();
listParam1.add("zhangsan");
listParam1.add("lisi");
listParam1.add(6);
/////
List<Object> listParam4=new ArrayList<>();
OrderInfo orderInfo1=new OrderInfo();
orderInfo1.setMoney(23);
orderInfo1.setId("NO.1");
OrderInfo orderInfo2=new OrderInfo();
orderInfo2.setMoney(24);
orderInfo2.setId("NO.2");
List listOrder=new ArrayList();
listOrder.add(orderInfo1);
listOrder.add(orderInfo2);
//[{"id":"NO.2","money":24},{"money":0}]
listParam4.add(JSON.toJSON(listOrder).toString());
List<Object> listParam6=new ArrayList<>();
listParam6.add("北京");
/*********************方法动态调用测试************************************/
for(int i=0;i<2;i++){
Long start=System.currentTimeMillis();
//多个参数情况
System.out.println(dynamicCallWebServiceByCXF(wsdlUrl,"sayHello2", "http://service.limp.com/","HelloServiceDemo",listParam1));
//单个对象
System.out.println(dynamicCallWebServiceByCXF(wsdlUrl,"selectOrderInfo", "http://service.limp.com/","HelloServiceDemo",listParam));
//多个对象
System.out.println(dynamicCallWebServiceByCXF(wsdlUrl,"selectOrderInfoAndOrderDetail", "http://service.limp.com/","HelloServiceDemo",listParam2));
//集合测试
System.out.println(dynamicCallWebServiceByCXF(wsdlUrl,"getOrderList", "http://service.limp.com/","HelloServiceDemo",listParam4));
//net创建的webservice通过其他方式获取
// System.out.println(dynamicCallWebServiceByCXF("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl","getSupportCity",
// "http://WebXml.com.cn/","",listParam6));
Long end=System.currentTimeMillis();
System.out.println(i+"调用用时"+(end-start));
}
}
转载从https://blog.****.net/zzhuan_1/article/details/84499188