CXF客户端(动态调用)
发布日期:2022-01-11 03:09:57 浏览次数:7 分类:技术文章

本文共 3098 字,大约阅读时间需要 10 分钟。

CXF下载:

所需包清单 含(服务端、客户端):

cxf-core-3.1.8

cxf-rt-bindings-soap-3.1.8

cxf-rt-databinding-jaxb-3.1.8

cxf-rt-frontend-jaxws-3.1.8

cxf-rt-frontend-simple-3.1.8

cxf-rt-transports-http-3.1.8

cxf-rt-transports-udp-3.1.8

cxf-rt-ws-addr-3.1.8

cxf-rt-wsdl-3.1.8

cxf-rt-ws-policy-3.1.8

neethi-3.0.3

slf4j-api-1.7.21

xmlschema-core-2.2.1

动态方式-demo:

public class GetWebServiceData {    private static final QName SERVICE_NAME = new QName(namespace, serviceName);    /**     *      * @throws Exception     */    public static void main(String[] args) throws Exception {        // 远程webService的URL        String hostUrl = "****************?wsdl";        try {            // 创建动态客户端            JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();            // 创建客户端连接            Client client = factory.createClient(hostUrl, SERVICE_NAME);            ClientImpl clientImpl = (ClientImpl) client;            Endpoint endpoint = clientImpl.getEndpoint();             ServiceInfo serviceInfo = endpoint.getService().getServiceInfos().get(0);            // 创建QName来指定NameSpace和要调用的service(bingdingName)            QName bindingName = new QName(namespace, service);            BindingInfo binding = serviceInfo.getBinding(bindingName);            // 创建QName来指定NameSpace和要调用的方法            QName opName = new QName(namespace, methodName);            BindingOperationInfo boi = binding.getOperation(opName);            BindingMessageInfo inputMessageInfo = boi.getInput();            List
parts = inputMessageInfo.getMessageParts(); // 取得对象实例 MessagePartInfo partInfo = parts.get(0); Class
partClass = partInfo.getTypeClass(); Object inputObject = partClass.newInstance(); //------------------反射 设置 参数(如果深层对象 要继续反射 往下) // 取得字段的set方法并赋值 PropertyDescriptor partPropertyDescriptor = new PropertyDescriptor(字段名,inputObject.getClass()); Method userNoSetter = partPropertyDescriptor.getWriteMethod(); userNoSetter.invoke(inputObject, 属性值); // 取得字段的set方法并赋值 PropertyDescriptor partPropertyDescriptor2 = new PropertyDescriptor(字段名, inputObject.getClass()); Method productCodeSetter = partPropertyDescriptor2.getWriteMethod(); productCodeSetter.invoke(inputObject, 属性值); //--------------------------------------------------------------- // 调用客户端invoke()方法,把inputObject传递给要调用的方法并取得结果对象 Object[] result = client.invoke(opName, inputObject); // 取得的结果是一个对象 Class
resultClass = result[0].getClass(); //------------------反射 获取 对象 (如果深层对象 要继续反射 往下) // 取得返回结果的get方法并得到它的值 PropertyDescriptor resultDescriptor = new PropertyDescriptor(结果字段名, resultClass); Object resultGetter = resultDescriptor.getReadMethod().invoke(result[0]); System.out.println("result:" + resultGetter); //-------------------------------------------------------------------- } catch (Exception e) { e.printStackTrace(); } }

转载地址:https://yuki-ho.blog.csdn.net/article/details/53195781 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Spring 服务启动 自动执行(ApplicationListener)
下一篇:Https协议详解

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2024年04月20日 14时21分41秒