java動態獲取WebService的兩種方式(復雜參數類型)
阿新 • • 發佈:2019-01-26
args oid 類型 ade poi 參數類型 pid eth lse
java動態獲取WebService的兩種方式(復雜參數類型)
第一種:
@Override public OrderSearchListRes searchOrderList(Order_FlightOrderSearchRequest request) { Object myAllMessage; OrderSearchListRes response = null; try { String endpoint = carGlobalSetting.getEndpoint(); JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); org.apache.cxf.endpoint.Client client = dcf .createClient(endpoint); QName name = new QName(carGlobalSetting.getNamespaceURI(), "searchOrderList"); Object person = Thread.currentThread().getContextClassLoader().loadClass("com.uni.webservice.service.neworder.inter.OrderSearchReq").newInstance(); Method m1 = person.getClass().getMethod("setSalesChannel", String.class); Method m2 = person.getClass().getMethod("setPassportId", Long.class); Method m3 = person.getClass().getMethod("setBeginDate", Integer.class); Method m4 = person.getClass().getMethod("setEndDate", Integer.class); Method m5 = person.getClass().getMethod("setOrderStatus", String.class); Method m6 = person.getClass().getMethod("setPage", Integer.class); Method m7 = person.getClass().getMethod("setPageSize", Integer.class); m1.invoke(person, request.getSalesChannel()); m2.invoke(person, request.getPassportId()); m3.invoke(person, request.getBeginDate()); m4.invoke(person, request.getEndDate()); m5.invoke(person, request.getOrderStatus()); m6.invoke(person, request.getPage()); m7.invoke(person, request.getPageSize()); try { myAllMessage = client.invoke(name, person); LogHelper.debug(myAllMessage.toString()); String s = JSON.toJSONString(myAllMessage); JSONArray jsonArray = JSON.parseArray(s); /** * 將Json轉為具體對象 */ for (Object o : jsonArray) { JSONObject j = (JSONObject) o; response = JSON.parseObject(j.toJSONString(), new TypeReference<OrderSearchListRes>() { }); } } catch (Exception e) { LogHelper.error("Json轉化異常"+e.getMessage()+e.getStackTrace(), "searchOrderList","searchOrderList"); } } catch (Exception e) { LogHelper.error("獲取WebService異常"+e.getMessage()+e.getStackTrace(), "searchOrderList","searchOrderList"); } return response; }
第二種:
private static String wsdlUrl = "http://172.20.29.51:8180/uniplatform/service/UniNewOrderDataService?wsdl"; public static void main(String[] args) throws Exception { // 創建動態客戶端 JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance(); Client client = factory .createClient(wsdlUrl); /**endpoint據說為http://172.20.29.51:8180/uniplatform/service/UniNewOrderDataService * 不過toString方法打印的為{},有點奇怪,不過getEndpointInfo打印的為BindingQName,ServiceQName,QName*/ Endpoint endpoint = client.getEndpoint(); /**獲取Service*/ ServiceInfo serviceInfo = endpoint.getService().getServiceInfos().get(0); /**創建Service*/ Collection<BindingInfo> bindings = serviceInfo.getBindings(); BindingInfo binding = null; for (BindingInfo b : bindings) { binding = b; } /**創建Service下的方法*/ QName opName = null; for (BindingOperationInfo bindingOperationInfo: binding.getOperations()) { if ("searchOrderList".equals(bindingOperationInfo.getName().getLocalPart())){ opName = bindingOperationInfo.getName(); } } BindingOperationInfo operation2 = binding.getOperation(opName); BindingMessageInfo input = null; if (operation2.isUnwrapped()){ input = operation2.getUnwrappedOperation().getInput(); } else { input = operation2.getWrappedOperation().getInput(); } List<MessagePartInfo> messageParts = input.getMessageParts(); MessagePartInfo messagePartInfo = messageParts.get(0); Class<?> partClass = messagePartInfo.getTypeClass(); Object inputObject = partClass.newInstance(); PropertyDescriptor partPropertyDescriptor = new PropertyDescriptor("salesChannel", partClass); partPropertyDescriptor.getWriteMethod().invoke(inputObject, "712"); PropertyDescriptor partPropertyDescriptor2 = new PropertyDescriptor("passportId", partClass); partPropertyDescriptor2.getWriteMethod().invoke(inputObject, Long.valueOf("31498882")); PropertyDescriptor partPropertyDescriptor3 = new PropertyDescriptor("beginDate", partClass); partPropertyDescriptor3.getWriteMethod().invoke(inputObject, 20181230); PropertyDescriptor partPropertyDescriptor4 = new PropertyDescriptor("endDate", partClass); partPropertyDescriptor4.getWriteMethod().invoke(inputObject, 20190109); PropertyDescriptor partPropertyDescriptor5 = new PropertyDescriptor("orderStatus", partClass); partPropertyDescriptor5.getWriteMethod().invoke(inputObject, "10054"); Object[] result = client.invoke(opName, inputObject); }
最後,返回的Object類型數據還是只能先將其轉為Json,再將Json轉化為對象,沒辦法直接拿到。
需要的兩個依賴:
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.2.6</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>3.2.7</version> </dependency>
java動態獲取WebService的兩種方式(復雜參數類型)