yangbin
2025-03-31 e70428eb12c33888fa36b680ad9582c95a50a56f
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
package org.jeecg.modules.msi.utils;
 
 
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
 
 
public class WebServiceUtil {
    /**
     * 1.代理类工厂的方式,需要拿到对方的接口地址, 同时需要引入接口
     */
    public static void invokeService_1(String address, Class<?> tClass){
        // 接口地址
        //String address = "http://localhost:8080/services/ws/api?wsdl";
        // 代理工厂
        JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
        // 设置代理地址
        jaxWsProxyFactoryBean.setAddress(address);
        // 设置接口类型
        jaxWsProxyFactoryBean.setServiceClass(tClass);
        // 创建一个代理接口实现
        Object us = jaxWsProxyFactoryBean.create();
        // 数据准备
        String data = "hello world";
        // 调用代理接口的方法调用并返回结果
        //String result = us.emrService(data);
        //System.out.println("返回结果:" + result);
    }
 
    /**
     * 3. 动态调用
     */
    public static String invokeService(String uri, String data, String method)
    {
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient(uri);
        Object[] objects = new Object[0];
        try {
            objects = client.invoke(method, data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return String.valueOf(objects[0]);
    }
 
    /**
     * 2. 动态调用
     */
    public static String invokeServiceObJect(String uri,Object[] datas, String method)
    {
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient(uri);
        Object[] objects = new Object[0];
        try {
            objects = client.invoke(method, datas);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return String.valueOf(objects[0]);
    }
}