java调用webservice接口

1.先在pom.xml增加依赖 

<dependency>
            <groupId>org.apache.axis</groupId>
            <artifactId>axis</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis</groupId>
            <artifactId>axis-jaxrpc</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>axis</groupId>
            <artifactId>axis-wsdl4j</artifactId>
            <version>1.5.1</version>
        </dependency>
        <dependency>
            <groupId>commons-discovery</groupId>
            <artifactId>commons-discovery</artifactId>
            <version>0.2</version>
        </dependency>

2.编写一个工具类

package org.hcoud.cloud.wuhuan.utils;

import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.axis.encoding.ser.BeanDeserializerFactory;
import org.apache.axis.encoding.ser.BeanSerializerFactory;
import org.apache.axis.message.SOAPHeaderElement;
import org.apache.axis.types.Schema;
import org.apache.commons.collections4.MapUtils;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import java.util.List;
import java.util.Map;
import java.util.Set;


@Slf4j
public class WebServiceUtils {
    // 接口调用地址
    private static String url = "http://xxxxxx.asmx";
    // 命名空间
    private static String namespace = "http://xxxxxxx/";

    /**
     * WebService - 调用接口
     *
     * @param methodName 函数名
     * @param params     参数
     * @param clazz      返回对象class
     * @return 返回结果(Object)
     */
    public static <T> T call(String methodName, Map<String, String> params, Class<T> clazz) {
        log.info("调用 WebService 发送参数==>" + JsonUtil.object2Json(params));
        String soapActionURI = namespace + methodName;
        try {
            Service service = new Service();

            SOAPHeaderElement header = new SOAPHeaderElement(namespace, methodName);
            header.setNamespaceURI(namespace);

            Call call = (Call) service.createCall();
            call.setTargetEndpointAddress(url);

            call.setOperationName(new QName(namespace, methodName));

            // 添加参数
            List<String> parameterList = Lists.newArrayList();
            if (params != null) {
                Set<String> paramsKey = params.keySet();
                for (String key : paramsKey) {
                    call.addParameter(new QName(namespace, key), XMLType.XSD_STRING, ParameterMode.IN);
                    String pValue = MapUtils.getString(params, key);
                    header.addChildElement(key).setValue(pValue);
                    parameterList.add(pValue);
                }
            }
            call.setUseSOAPAction(true);
            call.setSOAPActionURI(soapActionURI);
            call.addHeader(header);

            // 进行序列化  实体类也要序列化 implements Serializable
            call.registerTypeMapping(clazz, new QName(namespace, soapActionURI),
                    new BeanSerializerFactory(clazz, new QName(namespace, soapActionURI)),
                    new BeanDeserializerFactory(clazz, new QName(namespace, soapActionURI)));
            // 设置输出的类
            call.setReturnClass(clazz);
            // 接口返回结果
            T result = (T) call.invoke(parameterList.toArray());
            log.info("调用 WebService 接口返回===>" + result);
            return result;
        } catch (Exception e) {
            log.error("调用 WebService 接口错误信息==>" + e.getMessage());
        }
        return null;
    }

    /**
     * WebService - 接口调用
     *
     * @param methodName 函数名
     * @param params     参数
     * @return 返回结果(String)
     */
    public static String call(String methodName, Map<String, String> params) {
        log.info("调用 WebService 发送参数==>" + JsonUtil.object2Json(params));
        String soapActionURI = namespace + methodName;
        try {
            Service service = new Service();

            SOAPHeaderElement header = new SOAPHeaderElement(namespace, methodName);
            header.setNamespaceURI(namespace);

            Call call = (Call) service.createCall();
            call.setTargetEndpointAddress(url);

            call.setOperationName(new QName(namespace, methodName));

            // 添加参数
            List<String> parameterList = Lists.newArrayList();
            if (params != null) {
                Set<String> paramsKey = params.keySet();
                for (String key : paramsKey) {
                    call.addParameter(new QName(namespace, key), XMLType.XSD_STRING, ParameterMode.IN);
                    String pValue = MapUtils.getString(params, key);
                    header.addChildElement(key).setValue(pValue);
                    parameterList.add(pValue);
                }
            }
            call.setUseSOAPAction(true);
            call.setSOAPActionURI(soapActionURI);
            call.addHeader(header);
            // 设置返回类型
            call.setReturnType(new QName(namespace, methodName), String.class);
            // 接口返回结果
            String result = (String) call.invoke(parameterList.toArray());
            log.info("调用 WebService 接口返回===>" + result);
            return result;
        } catch (Exception e) {
            log.error("调用 WebService 接口错误信息==>" + e.getMessage());
        }
        return null;
    }

    /**
     * WebService - 调用接口
     *
     * @param methodName 函数名
     * @param params     参数
     * @return 返回结果(String)
     */
    public static String call2(String methodName, Map<String, String> params) {
        log.info("调用 WebService 发送参数==>" + JsonUtil.object2Json(params));
        String soapActionURI = namespace + methodName;
        try {
            Service service = new Service();

            SOAPHeaderElement header = new SOAPHeaderElement(namespace, methodName);
            header.setNamespaceURI(namespace);

            Call call = (Call) service.createCall();
            call.setTargetEndpointAddress(url);

            call.setOperationName(new QName(namespace, methodName));

            // 添加参数
            List<String> parameterList = Lists.newArrayList();
            if (params != null) {
                Set<String> paramsKey = params.keySet();
                for (String key : paramsKey) {
                    call.addParameter(new QName(namespace, key), XMLType.XSD_STRING, ParameterMode.IN);
                    String pValue = MapUtils.getString(params, key);
                    header.addChildElement(key).setValue(pValue);
                    parameterList.add(pValue);
                }
            }
            call.setUseSOAPAction(true);
            call.setSOAPActionURI(soapActionURI);
            call.addHeader(header);
            // 设置返回类型
            call.setReturnType(XMLType.XSD_SCHEMA);
            // 接口返回结果
            Schema schemaResult = (Schema)call.invoke(parameterList.toArray());
            String result = "";
            for(int i = 0; i<schemaResult.get_any().length; i++){
                result = result + schemaResult.get_any()[i];
            }
            log.error("调用 WebService 接口返回===>" + result);
            return result;
        } catch (Exception e) {
            log.error("调用 WebService 接口错误信息==>" + e.getMessage());
        }
        return null;
    }
}

3.调用示例:

Map<String,String> m = new HashMap<>();
m.put("参数名","参数值");
m.put("参数名","参数值");
WebServiceUtils.call("writeBatchProPackageNo",m);

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值