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的两种方式(复杂参数类型)的更多相关文章

  1. Java执行groovy脚本的两种方式

    记录Java执行groovy脚本的两种方式,简单粗暴: 一种是通过脚本引擎ScriptEngine提供的eval(String)方法执行脚本内容:一种是执行groovy脚本: 二者都通过Invocab ...

  2. Java中HashMap遍历的两种方式

    Java中HashMap遍历的两种方式 转]Java中HashMap遍历的两种方式原文地址: http://www.javaweb.cc/language/java/032291.shtml 第一种: ...

  3. java中数组复制的两种方式

    在java中数组复制有两种方式: 一:System.arraycopy(原数组,开始copy的下标,存放copy内容的数组,开始存放的下标,需要copy的长度); 这个方法需要先创建一个空的存放cop ...

  4. MyBatis获取参数值的两种方式

    MyBatis获取参数值的两种方式:${}和#{} ${}的本质就是字符串拼接,#{}的本质就是占位符赋值 ${}使用字符串拼接的方式拼接sql,若为字符串类型或日期类型的字段进行赋值时,需要手动加单 ...

  5. WebService的两种方式Soap和Rest比较

    我的读后感:由于第一次接触WebService,对于很多概念不太理解,尤其是看到各个OpenAPI的不同提供方式时,更加疑惑.如google map api采用了AJAX方式,通过javascript ...

  6. WebService的两种方式SOAP和REST比较 (转)

    我的读后感:由于第一次接触WebService,对于很多概念不太理解,尤其是看到各个OpenAPI的不同提供方式时,更加疑惑.如google map api采用了AJAX方式,通过javascript ...

  7. cxf构建webservice的两种方式

    一.简介 对于基于soap传输协议的webservice有两种开发模式,代码优先和契约优先的模式.代码优先的模式是通过编写服务器端的代码,使用代码生成wsdl:契约优先模式首先编写wsdl,再通过ws ...

  8. java中实现同步的两种方式:syschronized和lock的区别和联系

    Lock是java.util.concurrent.locks包下的接口,Lock 实现提供了比使用synchronized 方法和语句可获得的更广泛的锁定操作,它能以更优雅的方式处理线程同步问题,我 ...

  9. Java值创建线程的两种方式对比

    在Java中创建线程的方式有两种,第一种是直接继承Thead类,另一种是实现Runable接口.那么这两种方式孰优孰劣呢? 采用继承Thead类实现多线程: 优势:编写简单,如果需要访问当前线程,只需 ...

随机推荐

  1. 在Docker容器中搭建MXNet/Gluon开发环境

    在这篇文章中没有直接使用MXNet官方提供的docker image,而是从一个干净的nvidia/cuda镜像开始,一步一步部署mxnet需要的相关软件环境,这样做是为了更加细致的了解mxnet的运 ...

  2. KVM虚拟化概述与安装

    虚拟化是构建云计算基础架构不可或缺的关键技术之一,云计算的云端系统,其实质上就是一个大型的KVM分布式系统,虚拟化通过在一个物理平台上虚拟出更多的虚拟平台,而其中的每一个虚拟平台则可以作为独立的终端加 ...

  3. postgresql 安装文档

    tar xf postgresql-9.4.5.tar.gz cd postgresql-9.4.5 yum grouplist yum grouplist|grep Deve yum groupin ...

  4. mysql原生sql盘点

    select*from (select*from test1 union all select*from test2 ) //两个查询的数据行数需要对应一致,且名字as 一致. 1.如果直接用如下sq ...

  5. VS Code 快捷键大全

    前言 VSCode的快捷键继承了一些IDE风格,有VS的身影,也有Emacs的身影..简言之,内置快捷键玩熟了,效率提高不是一点两点. VsCode 快捷键有五种组合方式(科普) 通用快捷键 基础编辑 ...

  6. PHP之ThinkPHP框架(数据库)

    PHP是网站后台开发语言,其重要的操作对象莫过于数据库,之前有了解过mysqli和pdo,但ThinkPHP的数据库交互必须使用其特定的封装方法,或者可以认为其是对PHP数据库操作的进一步封装,以达到 ...

  7. ES6的Promise

    推荐一下我觉得不错关于Promise的好文章,通俗易懂 说起ES6的Promise就要提及一下JQ的$.when()方法,两者基本相同 面试的时候经常会问Promise,如果同学们能在回答Promis ...

  8. STL::sort函数实现

    声明:本文参考链接:STL::sort实现. 排序是面试中经常被问及的算法基础知识点,虽然实际应用中不会直接使用,但是理解这些简单的算法知识对于更复杂更实用的算法有一定的帮助,毕竟面试总不能问的太过深 ...

  9. 原生端与服务器通过sessionid实现session共享以及登录验证

    注:原生端与服务器建立连接时产生的sessionid会变,跟上一次的不一样,为了保证sessionid一样,所以第一次服务器需要把sessionid返回给原生端,下一次与服务端会话时,原生端需要把这个 ...

  10. eclipse 在jboss的debug配置(ubuntu系统)

    转自:https://blog.csdn.net/iteye_3878/article/details/81695877 由于我在ubuntu下权限设置分开,如 /home/jboss/ (jboss ...