使用axis2调用web service

本文详细介绍使用Axis2生成客户端代码的过程,包括下载与配置、命令行参数解析、客户端代码生成及项目集成步骤。并提供了如何通过Maven引入必要Jar包的示例,以及调用生成的客户端代码请求WSDL的具体代码示例。

使用axis2生成客户端代码

  1. 下载axis2 , 点击这里, 选择 Binary distribution
  2. 下载后解压,解压进入到bin目录
  3. 执行命令生成客户端代码

> wsdl2java --help // 查看帮助,命令选项可供参考

  • 该命令将所有的类都合并成了一个类(文件)

wsdl2java -uri http://localhost:8080/services/XXXService.svc?wsdl -p com.xxx.yyy -o d:\myCode

  • 该命令将接口涉及到的不同的接口入参/出参/返回/枚举等等拆分成了不同的类,并且文件较多

wsdl2java -uri http://your_soa_url -p com.xx.yy.zz -o d:\your_local_path -s -u

上面两种情况,具体怎么选择,由你决定。
建议: 如果通过方式1生成的文件少于3M, 可以选择1. 反之选择2.
因为当一个类文件超过一定大小后,IDEA会特别卡。

-uri: 后可以接wsdl连接,或者wsdl文件
-p : 指定生成的包名,可以和项目一致,比如你想将生成的代码放置于 com.example.test 里面,那么-p com.example.test
-o : 指定生成代码存放在本地的路径,看你个人喜好

  1. 生成客户端代码后,将代码拷贝到你项目中

引入jar包

通过maven引入

        <!-- https://mvnrepository.com/artifact/org.apache.axis2/axis2-kernel -->
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-kernel</artifactId>
            <version>1.7.8</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.axis2/axis2-adb -->
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-adb</artifactId>
            <version>1.7.8</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.axis2/axis2-transport-local -->
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-transport-local</artifactId>
            <version>1.7.8</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.axis2/axis2-transport-http -->
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-transport-http</artifactId>
            <version>1.7.8</version>
        </dependency>

调用客户端代码请求wsdl

部分代码经过了脱敏处理

String url = "http://localhost:8080/services/XXXService.svc";
// 下面的内容基本都是依葫芦画瓢即可
AxxServiceStub stub = new AxxServiceStub(url);
Options options = stub._getServiceClient().getOptions();
options.setTimeOutInMilliSeconds(100000);
stub._getServiceClient().setOptions(options);
// 初始化请求入参
AxxServiceStub.InvestReq investReq = new AxxServiceStub.InvestReq();
// set请求字段数据
investReq.setAccount(1);
// 这里需要留意一下
// Source是服务端自定义字段
investReq.setSource(AxxServiceStub.ClientToInvestType.PC);

AxxServiceStub.Invest invest = new AxxServiceStub.Invest();
invest.setReq(investReq);
AxxServiceStub.InvestResp response = stub.invest(invest).getInvestResult();

log.info("调用xx返回结果:" + response.getMessage());
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值