Java使用axis调用webservice接口方法

import jdk.nashorn.internal.parser.JSONParser;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import javax.xml.namespace.QName;
import java.rmi.RemoteException;

public class HttpClassDemo {
    public static void main(String[] args) throws Exception {

        Object[] key = {方法参数名};
        Object[] val = {方法参数值};
        invokeMethod(url,方法名,key,val);
    }

    public static String invokeMethod(String url,String methodName, Object[] keys, Object[] values)  throws Exception{

        Service service = new Service();
        Call call;
        String result = "";
        try {
            call = (Call) service.createCall();
            call.setTargetEndpointAddress(url);
            //new QName的URL是要指向的命名空间的名称,这个URL地址在你的wsdl打开后可以看到的,
            //上面有写着targetNamespace="http://*.*.*/",这个就是你的命名空间值了;
            //call.setOperationName(new QName("url", "methodName")); // 调用的方法名
            call.setOperationName(methodName);
            if (keys.length==values.length){
                for (int i = 0; i < keys.length; i++) {
                //这里也可以直接写有几个写几个  
                // 设置参数名 :参数名 ,参数类型:String, 参数模式:'IN' or 'OUT'
                    call.addParameter(String.valueOf(keys[i]), org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
                }
                call.setEncodingStyle("UTF-8");
                call.setReturnType(XMLType.XSD_STRING);
                result = (String) call.invoke(values);// 远程调用
            }else {
                result="参数不匹配";
            }
         

            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }


}

pom依赖

<dependencies>
    <dependency>
        <groupId>org.apache.axis</groupId>
        <artifactId>axis</artifactId>
        <version>1.4</version>
    </dependency>
    <dependency>
        <groupId>wsdl4j</groupId>
        <artifactId>wsdl4j</artifactId>
        <version>1.6.3</version>
    </dependency>
    <dependency>
        <groupId>axis</groupId>
        <artifactId>axis-jaxrpc</artifactId>
        <version>1.4</version>
    </dependency>
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>commons-discovery</groupId>
        <artifactId>commons-discovery</artifactId>
        <version>0.5</version>
    </dependency>
</dependencies>
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值