WebService客户端Axis2调用

1.RPC方式调用

RPCServiceClient方式不能携带指定参数,下列为ServiceClient方式:

public static void main(String[] args) {
        try {
            String soapBindingAddress = "http://192.168.10.1:8080/BillService.asmx?wsdl";

            ServiceClient sender = new ServiceClient();
            EndpointReference endpointReference = new EndpointReference(soapBindingAddress);
            Options options = new Options();
	    // 命名空间加方法名
            options.setAction("http://tempuri.org/PuBillVouchInterface");
            options.setTo(endpointReference);
            sender.setOptions(options);

            OMFactory fac = OMAbstractFactory.getOMFactory();
            // 设置命名空间
            OMNamespace omNs = fac.createOMNamespace("http://tempuri.org/", "PuBillVouchInterface");
            OMElement data = fac.createOMElement("PuBillVouchInterface", omNs);

            
            String dataXml = "" +
                    "" +
                        "" +
                            "AB002324E1" +
                            "2016-7-22" +
                            "2" +
                            "" +
                                "1" +
                                "12.2" +
                                "12.2" +
                            "" +
                            "" +
                                "12" +
                                "30" +
                                "360" +
                            "" +
                        "" +
                    "";

            String md5 = DigestUtils.md5Hex("md5key" + dataXml).toUpperCase();

            // 对应参数的节点
            String[] strs = new String[]{"dataXml","certificateHeader"};
            // 参数值
            String[] val = new String[]{dataXml, md5};
            for (int i = 0; i < strs.length; i++) {
                OMElement inner = fac.createOMElement(strs[i], omNs);
                inner.setText(val[i]);
                data.addChild(inner);
            }
            // 发送数据,返回结果
            OMElement result = sender.sendReceive(data);
            System.out.println(result.toString());
            System.out.println(result.getFirstElement().getText());
        } catch (AxisFault ex) {
            ex.printStackTrace();
        }
    }

2.借助idea生成类文件进行调用

首先下载axis2包文件,地址:http://axis.apache.org/axis2/java/core/download.cgi 

解压后配置如下:

WebService客户端Axis2调用_第1张图片


在项目某目录下,右键-->WebService-->Generate Java Code From Wsdl,按提示配置生成文件即可。生成YBServiceCallbackHandler.java、YBServiceStub两个文件

调用示例如下:

public static void main(String[] args) {
        try {
            String dataXml = "" +
                    "" +
                        "" +
                            "1322k322g2301" +
                            "2016-7-8"+
                        "" +
                    "";

            String md5 = DigestUtils.md5Hex("md5key" + dataXml).toUpperCase();

            YBServiceStub stub = new YBServiceStub("http://192.168.10.1:8080/BillService.asmx?wsdl");
            YBServiceStub.PuBillVouchInterface request = new YBServiceStub.PuBillVouchInterface();

            request.setCertificateHeader(md5);
            request.setDataXml(dataXml);
            YBServiceStub.PuBillVouchInterfaceResponse rs = stub.puBillVouchInterface(request);
            System.out.println(rs.getPuBillVouchInterfaceResult());

        } catch (Exception axisFault) {
            axisFault.printStackTrace();
        }
    }


需要Axis2相关依赖:

 
  

    1.6.2




org.apache.axis2
    axis2
    ${axis2.version}



org.apache.axis2
    axis2-transport-http
    ${axis2.version}



org.apache.axis2
    axis2-transport-local
    ${axis2.version}



    org.apache.xmlbeans
    xmlbeans
    2.4.0



    org.apache.ws.commons.axiom
    axiom-api
    1.2.13




 
  

你可能感兴趣的:(架构设计)