Java 调用 WebService

Java EE 目录:https://blog.youkuaiyun.com/dkbnull/article/details/87932809

1. Axis 调用 WebService

1.1 引入依赖

        <dependency>
            <groupId>org.apache.axis</groupId>
            <artifactId>axis</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>axis</groupId>
            <artifactId>axis-jaxrpc</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>commons-discovery</groupId>
            <artifactId>commons-discovery</artifactId>
            <version>0.2</version>
        </dependency>
        <dependency>
            <groupId>wsdl4j</groupId>
            <artifactId>wsdl4j</artifactId>
            <version>1.6.3</version>
        </dependency>
        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>javax.mail</artifactId>
            <version>1.5.6</version>
        </dependency>

1.2 调用WebService

    /**
     * 调用 WebService 服务
     *
     * @param url       请求地址
     * @param namespace 命名空间
     * @param method    接口方法
     * @param actionURI SOAPActionURI
     * @param params    请求参数
     * @param timeout   超时时间
     * @param clazz     返回参数类型
     * @param <T>       接收参数类型
     * @return
     * @throws Exception
     */
    public static <T> T callWebService(String url, String namespace, String method, String actionURI, Map<String, Object> params, int timeout, Class<T> clazz) throws Exception {
        Service service = new Service();
        Call call = (Call) service.createCall();
        call.setTargetEndpointAddress(url);
        call.setOperationName(new QName(namespace, method));
        if (!StringUtils.isEmpty(actionURI)) {
            call.setSOAPActionURI(actionURI);
        }
        call.setTimeout(timeout);

        List<String> keys = new ArrayList<>(params.keySet());
        String[] values = new String[params.size()];
        for (int i = 0; i < params.size(); i++) {
            call.addParameter(new QName(namespace, keys.get(i)), XMLType.XSD_STRING, ParameterMode.IN);
            values[i] = params.get(keys.get(i)).toString();
        }

        call.registerTypeMapping(clazz,
                new QName(namespace, method),
                new BeanSerializerFactory(clazz, new QName(namespace, method)),
                new BeanDeserializerFactory(clazz, new QName(namespace, method)));
        if (!StringUtils.isEmpty(actionURI)) {
            call.setUseSOAPAction(true);
        }

        if (clazz.getName().contains("java.lang.String")) {
            call.setReturnType(XMLType.XSD_STRING);
        } else {
            call.setReturnClass(clazz);
        }

        return (T) call.invoke(values);
    }
}

2. Cxf 调用 WebService

2.1 引入依赖

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>3.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>3.3.0</version>
        </dependency>

2.2 调用WebService

    /**
     * 调用 WebService 服务
     *
     * @param url       请求地址
     * @param namespace 命名空间
     * @param method    接口方法
     * @param params    请求参数
     * @return
     * @throws Exception
     */
    public static Object callWebService(String url, String namespace, String method, Map<String, Object> params) throws Exception {
        JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory.newInstance();
        Client client = clientFactory.createClient(url);
        Object[] objects = client.invoke(new QName(namespace, method), params.toString());

        return objects[0];
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值