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();
<span style="white-space:pre">	</span>    // 命名空間加方法名
            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 = "<?xml version='1.0' encoding='UTF-8'?>" +
                    "<DataTable>" +
                        "<DataRow>" +
                            "<bill_no>AB002324E1</bill_no>" +
                            "<ddate>2016-7-22</ddate>" +
                            "<cBillType>2</cBillType>" +
                            "<DataDetails>" +
                                "<iquantity>1</iquantity>" +
                                "<iprice>12.2</iprice>" +
                                "<isum>12.2</isum>" +
                            "</DataDetails>" +
                            "<DataDetails>" +
                                "<iquantity>12</iquantity>" +
                                "<iprice>30</iprice>" +
                                "<isum>360</isum>" +
                            "</DataDetails>" +
                        "</DataRow>" +
                    "</DataTable>";

            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-->Generate Java Code From Wsdl,按提示配置生成文件即可。生成YBServiceCallbackHandler.java、YBServiceStub兩個文件

調用示例如下:

public static void main(String[] args) {
        try {
            String dataXml = "<?xml version='1.0' encoding='UTF-8'?>" +
                    "<DataTable>" +
                        "<DataRow>" +
                            "<bill_no>1322k322g2301</bill_no>" +
                            "<ddate>2016-7-8</ddate>"+
                        "</DataRow>" +
                    "</DataTable>";

            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相關依賴:

<properties>
    <axis2.version>1.6.2</axis2.version>
</properties>

<!-- axis2 -->
<dependency>
<groupId>org.apache.axis2</groupId>
    <artifactId>axis2</artifactId>
    <version>${axis2.version}</version>
</dependency>

<dependency>
<groupId>org.apache.axis2</groupId>
    <artifactId>axis2-transport-http</artifactId>
    <version>${axis2.version}</version>
</dependency>

<dependency>
<groupId>org.apache.axis2</groupId>
    <artifactId>axis2-transport-local</artifactId>
    <version>${axis2.version}</version>
</dependency>

<dependency>
    <groupId>org.apache.xmlbeans</groupId>
    <artifactId>xmlbeans</artifactId>
    <version>2.4.0</version>
</dependency>
<!-- 1.2.10會有異常 -->
<dependency>
    <groupId>org.apache.ws.commons.axiom</groupId>
    <artifactId>axiom-api</artifactId>
    <version>1.2.13</version>
</dependency>




發佈了107 篇原創文章 · 獲贊 61 · 訪問量 38萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章