• 使用axis调用WebService,Java WebService调用工具类


    WebService 简介

    概述

    能使得运行在不同机器上的不同应用无须借助附加的、专门的第三方软件或硬件,就可相互交换数据或集成。依据 WebService 规范实施的应用之间,无论它们所使用的语言、平台或内部协议是什么,都可以相互交换数据。

    也就是说可以实现多语言、跨平台数据交互。

    使用 axis 调用 WebService

    引入依赖

    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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    WebService 调用工具类

    package com.micromaple.common.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.collections.MapUtils;
    
    import javax.xml.namespace.QName;
    import javax.xml.rpc.ParameterMode;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    
    /**
     * WebService - 工具类
     * Title: WebServiceUtils
     * Description:
     *
     * @author Micromaple
     * @version 1.0.0
     * @date 2022/7/1 12:14
     */
    @Slf4j
    public class WebServiceUtils {
        /**
         * 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 发送参数==>" + MapperUtils.mapToJson(params));
            String url = "http://127.0.0.1:8080/webservice.asmx";
            String namespace = "http://tempuri.org/";
            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 发送参数==>" + MapperUtils.mapToJson(params));
            String url = "http://127.0.0.1:8080/webservice.asmx";
            String namespace = "http://tempuri.org/";
            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 发送参数==>" + MapperUtils.mapToJson(params));
            String url = "http://127.0.0.1:8080/webservice.asmx";
            String namespace = "http://tempuri.org/";
            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;
        }
    }
    
    • 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
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 第一个方法为返回对象的方式,区别在于需要定义返回对象,属性值需要跟 WebService 中的返回对象保持一致。
    • 第二个方法适用于调用 WebService后,返回的值为 String 类型。

    参数详解:

    methodName:函数名。需要调用的 WebService 的函数名称。

    params:参数。调用 WebService 时需传入的参数。

  • 相关阅读:
    android7.1 系统ota升级与升级失败解决方法
    有梦想就去追,程序员辞职组乐队被老板资助
    VectorDraw Developer Framework 10.1001 Crack
    ESP 特权隔离机制介绍
    Spring Boot整合Postgres实现轻量级全文搜索
    ffmpeg播放时刻与视频文件时间戳对齐(同步)
    每日一练——快速合并2个有序数组
    tensorflow代码翻译成pytorch代码 -详细教程+案例
    Google Authenticator 和gitlab使用的方法配置Google AuthenticatorGoogle
    8.7 typedef关键字
  • 原文地址:https://blog.csdn.net/qq_41779565/article/details/125564485