java动态获取WebService的两种方式(复杂参数类型)
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的两种方式(复杂参数类型)的更多相关文章
- Java执行groovy脚本的两种方式
记录Java执行groovy脚本的两种方式,简单粗暴: 一种是通过脚本引擎ScriptEngine提供的eval(String)方法执行脚本内容:一种是执行groovy脚本: 二者都通过Invocab ...
- Java中HashMap遍历的两种方式
Java中HashMap遍历的两种方式 转]Java中HashMap遍历的两种方式原文地址: http://www.javaweb.cc/language/java/032291.shtml 第一种: ...
- java中数组复制的两种方式
在java中数组复制有两种方式: 一:System.arraycopy(原数组,开始copy的下标,存放copy内容的数组,开始存放的下标,需要copy的长度); 这个方法需要先创建一个空的存放cop ...
- MyBatis获取参数值的两种方式
MyBatis获取参数值的两种方式:${}和#{} ${}的本质就是字符串拼接,#{}的本质就是占位符赋值 ${}使用字符串拼接的方式拼接sql,若为字符串类型或日期类型的字段进行赋值时,需要手动加单 ...
- WebService的两种方式Soap和Rest比较
我的读后感:由于第一次接触WebService,对于很多概念不太理解,尤其是看到各个OpenAPI的不同提供方式时,更加疑惑.如google map api采用了AJAX方式,通过javascript ...
- WebService的两种方式SOAP和REST比较 (转)
我的读后感:由于第一次接触WebService,对于很多概念不太理解,尤其是看到各个OpenAPI的不同提供方式时,更加疑惑.如google map api采用了AJAX方式,通过javascript ...
- cxf构建webservice的两种方式
一.简介 对于基于soap传输协议的webservice有两种开发模式,代码优先和契约优先的模式.代码优先的模式是通过编写服务器端的代码,使用代码生成wsdl:契约优先模式首先编写wsdl,再通过ws ...
- java中实现同步的两种方式:syschronized和lock的区别和联系
Lock是java.util.concurrent.locks包下的接口,Lock 实现提供了比使用synchronized 方法和语句可获得的更广泛的锁定操作,它能以更优雅的方式处理线程同步问题,我 ...
- Java值创建线程的两种方式对比
在Java中创建线程的方式有两种,第一种是直接继承Thead类,另一种是实现Runable接口.那么这两种方式孰优孰劣呢? 采用继承Thead类实现多线程: 优势:编写简单,如果需要访问当前线程,只需 ...
随机推荐
- 2017-12-04 编写Visual Studio Code插件初尝试
参考官方入门: Your First Visual Studio Code Extension - Hello World 源码在: program-in-chinese/vscode_helloWo ...
- audacity 做音频分析之--初相识
软件介绍: Audacity是一个跨平台的声音编辑软件,用于录音和编辑音频,是自由.开放源代码的软件.可在Mac OS X.Microsoft Windows.GNU/Linux和其它操作系统上运作. ...
- webpack vue-loader was used without the corresponding plugin. Make sure to include VueLoaderPlugin
当我们出现以下报错! 解决方案: // webpack配置文件 const path = require('path'); const htmlWebpackPlugin = require('htm ...
- asp.net MVC 上传文件 System.Web.HttpException: 超过了最大请求长度
APS.NET MVC 上传文件出现 System.Web.HttpException: 超过了最大请求长度 这个问题 原因是 默认最大上传文件大小为4096,而我提交的文件太大了. 解决方案:修改 ...
- .Net Self Hosting 的几种方式
写在前面: IIS是Windows平台非常关键的组件,它是微软自带的Web服务器,可以很方便的帮助我们运行起一个网站,WebApi等服务,提供给外部来访问.即使它被很多java或者ruby的同学各种鄙 ...
- Jenkins的一些笔记
公司主要要开发自己的paas平台,集成了Jenkins,真的是遇到了很多很多困难,特别是在api调用的权限这一块,这里,把自己遇到的一些坑的解决方法做一下笔记吧.当然,首先要讲的,就是如何在开启安全的 ...
- linux 命令 — sort、uniq
sort uniq sort:对行或者文本文件排序 uniq:去除重复的行 常用 sort -n file.txt 按数字进行排序 sort -r file.txt 按逆序进行排序 sort -M f ...
- 2.Magicodes.NET框架之路——策略管理
闲话策略 策略,有很多解释.但鄙人个人比较看重这点: 策略,是为了实现某个目标或者针对某些问题而制定的应对方案,以最终实现目标.比如为实现生娃而XXOO. 因此在本框架中,策略(Strategy),则 ...
- 【Vue.js】vue引入组件报错:该组件未注册?
[Vue warn]: Unknown custom element: <QuestionnaireOption> - did you register the component cor ...
- MyBatis源码解析(十二)——binding绑定模块之MapperRegisty
原创作品,可以转载,但是请标注出处地址:http://www.cnblogs.com/V1haoge/p/6758456.html 1.回顾 之前解析了解析模块parsing,其实所谓的解析模块就是为 ...