使用axis远程调用webservice接口

以下是基于Maven项目的客户端调用

第一步:Maven项目需要导入对应的axis依赖如下:

<!-- 引入axis调用远程webservice相关jar依赖 by dhj-->
<dependency>
    <groupId>org.apache.axis</groupId>
    <artifactId>axis</artifactId>
    <version>1.4</version>
</dependency>

<dependency>
    <groupId>commons-discovery</groupId>
    <artifactId>commons-discovery</artifactId>
    <version>0.4</version>
    <exclusions>
        <exclusion>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>org.apache.axis</groupId>
    <artifactId>axis-jaxrpc</artifactId>
    <version>1.4</version>
</dependency>
<dependency>
    <groupId>org.apache.axis</groupId>
    <artifactId>axis-saaj</artifactId>
    <version>1.4</version>
</dependency>

<dependency>
    <groupId>wsdl4j</groupId>
    <artifactId>wsdl4j</artifactId>
    <version>1.6.2</version>
</dependency>

 

第二步:客户端java调用通用方法:

//通过axis方式调用webservice接口
    public static void callWebservice() {
        try {
            // 指出service所在完整的URL
            String endpoint = "http://ip:端口号/项目名/webservice/sei(即webservice接口名)?wsdl";
            //调用接口的targetNamespace
            String targetNamespace = "http://webservice接口所在的包名,逆序,一直到src下";(命名空间可以通过浏览器打开你的wsdl地址,里面对应的名称)
            //所调用接口的方法method
            String method = "所要调用的方法名";
            // 创建一个服务(service)调用(call)
            Service service = new Service();
            Call call = (Call) service.createCall();// 通过service创建call对象
            // 设置service所在URL
            call.setTargetEndpointAddress(new java.net.URL(endpoint));
            call.setOperationName(new QName(targetNamespace, method));
            call.setUseSOAPAction(true);
            //变量最好只是用String类型,其他类型会报错
            call.addParameter(new QName(targetNamespace, "变量名"), org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN);//设置参数名 state  第二个参数表示String类型,第三个参数表示入参  
            call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);// 设置返回类型
            // String path = targetNamespace + method;
            // call.setSOAPActionURI(path);
            String jsonString = (String) call.invoke(new Object[] {"变量值"});//此处为数组,有几个变量传几个变量
            //将json字符串转换为JSON对象
            JSON json = (JSON) JSON.parse(jsonString);
            //将接送对象转为java对象,此处用object代替,用的时候转换为你需要是用的对象就行了
            Object object = JSON.toJavaObject(json, Object.class);//注意别到错包com.alibaba.fastjson.JSON
            System.out.println(jsonString);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

--------------------- 
 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值