webservice之axis2工具wsdl2java使用

  1. 解压axis2-1.7.8-bin.zip,看到bin目录
  2. 增加maven的依赖:需要删除servlet-api包,否则,如果项目引用了3.0以上的servlet-api会不起作用。
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-kernel</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>javax.servlet</groupId>
                    <artifactId>servlet-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-adb</artifactId>
            <version>${axis2.version}</version>
        </dependency>

3 进入bin目录,执行命令

wsdl2java -uri http://localhost:8080/EverParentManage/services/sayHello?wsdl -p com.eversec.protect.modules.webservices.client.test -o stub


参数解释:-p 是生成代码的包路径,-o 是代码的生成目录。

这里写图片描述
4. 在当前目录生成stub文件夹,打开,拷贝两个文件到项目中,修改报错(主要是包名)
5. 新建一个SayHelloClient类,用来调用stub

package com.nansw.protect.modules.webservices.client.test;

public class SayHelloClient {

    public static void sayHello() {
        try {
            String url = "http://localhost:8080/ProManage/services/sayHello";
            SayHelloStub stub = new SayHelloStub(url);
            SayHelloStub.SayHello sayHello = new SayHelloStub.SayHello();
            sayHello.setName("张三");
            SayHelloStub.SayHelloResponse response = stub.sayHello(sayHello);
            String result = response.get_return();
            System.out.println(result);
        } catch (Exception axisFault) {
            axisFault.printStackTrace();
        }
    }

    public static void main(String[] args) {
        sayHello();
    }
}

6 修改SayHelloStub类,增加打印日志,请求报文和返回报文

//省略
            //adding SOAP soap_headers
            _serviceClient.addHeadersToEnvelope(env);
            // set the message context with that soap envelope
            _messageContext.setEnvelope(env);

            // add the message contxt to the operation client
            _operationClient.addMessageContext(_messageContext);
            System.out.println("请求报文:" + env);
            //execute the operation client
            _operationClient.execute(true);

            org.apache.axis2.context.MessageContext _returnMessageContext = _operationClient.getMessageContext(org.apache.axis2.wsdl.WSDLConstants.MESSAGE_LABEL_IN_VALUE);
            org.apache.axiom.soap.SOAPEnvelope _returnEnv = _returnMessageContext.getEnvelope();
            System.out.println("返回报文:" + _returnEnv);
            Object object = fromOM(_returnEnv.getBody()
                            .getFirstElement(),
                    SayHelloResponse.class);

            return (SayHelloResponse) object;
//省略

7.执行代码:
这里写图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值