不积跬步,无以至千里。不积小流,无以成江海。
对于WebService的调用,之前是使用CXF直接生成代理类的方式,这样的好处是拿到后可以直接调用,但生成的文件太多,并且包名需要与服务端相同,使用进起来感觉很重。
使用CXF动态客户端的方式去调用也很简单,并且不需要生成任何文件。
示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
| public class CxfApp {
private static Map<Object, Endpoint> factoryMap = new HashMap<>(); private static Map<Object, Client> clientMap = new HashMap<>();
public static void main(String[] args) { Gson gson = new Gson(); SysCategoryWbSReq sysCategoryWbSReq = new SysCategoryWbSReq(); sysCategoryWbSReq.setCategoryName("test20190920"); sysCategoryWbSReq.setCategoryCode("ML0025"); sysCategoryWbSReq.setEnabledFlag(1); List<Object> paramList = new ArrayList<>(); paramList.add(gson.toJson(sysCategoryWbSReq)); String result = dynamicCallWebServiceByCXF("http://127.0.0.1:9095/application/applicationWBS/addAndUpdateSyscategory?wsdl", "addAndUpdateSyscategory", "http://applicationWBS.expense.app.hcf.hand.com","ApplicationWbService",paramList); System.out.println(result); }
public static String dynamicCallWebServiceByCXF(String wsdlUrl, String methodName, String targetNamespace, String name, List<Object> paramList){ 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 endpoint=factoryMap.get(methodName); Client client=clientMap.get(methodName); ServiceInfo serviceInfo = endpoint.getService().getServiceInfos().get(0); String localPart=name+"SoapBinding"; QName bindingName = new QName(targetNamespace, localPart); BindingInfo binding = serviceInfo.getBinding(bindingName);
QName opName = new QName(targetNamespace, methodName);
BindingOperationInfo boi = binding.getOperation(opName);
BindingMessageInfo inputMessageInfo = null; if (!boi.isUnwrapped()) { 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(); System.out.println(partClass.getCanonicalName()); Object initDomain=null; 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; 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 "; } }
|