首頁 > Java > java教程 > 主體

SpringBoot如何使用axis呼叫webservice介面

王林
發布: 2023-05-11 13:28:06
轉載
2146 人瀏覽過

WebService

定義

SpringBoot如何使用axis呼叫webservice介面

個人理解

透過度娘等方式,個人理解為變相的soap協定加xml工單一處理,

實務

webservice 常識

一個webservice 介面發佈位址往往類似:

  • ##qq 線上驗證介面:

www.webxml.com.cn/webservices…

  • 其他可測試介面:

email電子郵件位址介面: www.webxml.com.cn/WebServices…

全國天氣狀況介面:

www.webxml.com.cn/WebServices…

qq線上介面驗證介面為例

在介面後面加:

/wsdl www.webxml.com.cn/webservices…

造訪查看然後找到下圖中定義的內容:注意使用關聯key找到對應的必要使用的參數。

SpringBoot如何使用axis呼叫webservice介面

maven 使用axis

應用依賴(不可缺少必須)

        <!-- https://mvnrepository.com/artifact/org.apache.axis/axis -->
        <dependency>
            <groupId>org.apache.axis</groupId>
            <artifactId>axis</artifactId>
            <version>1.4</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/wsdl4j/wsdl4j -->
        <dependency>
            <groupId>wsdl4j</groupId>
            <artifactId>wsdl4j</artifactId>
            <version>1.6.2</version>
        </dependency>
<!--        解决cell 转换问题-->
        <!-- https://mvnrepository.com/artifact/javax.xml/jaxrpc-api -->
        <dependency>
            <groupId>javax.xml</groupId>
            <artifactId>jaxrpc-api</artifactId>
            <version>1.1</version>
        </dependency>
<!--       解析调用结果以及数据转换包-->
        <!-- https://mvnrepository.com/artifact/commons-discovery/commons-discovery -->
        <dependency>
            <groupId>commons-discovery</groupId>
            <artifactId>commons-discovery</artifactId>
            <version>0.2</version>
        </dependency>
登入後複製

程式碼(貼上可用)

@Test
public void testWebService() {
    try {
        //wsdl地址
        String endpoint = "http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx";
        //命名空间
        String namespace = "http://WebXml.com.cn/";
        //服务名
        String serviceName = "qqOnlineWebService";
        //方法名
        String methodName = "qqCheckOnline";
        //soapAction
        String soapAction = "http://WebXml.com.cn/qqCheckOnline";

        Service service = new Service();
        Call call = (Call) service.createCall();
        //设置响应超时
        call.setTimeout(3000);
        //设置地址
        call.setTargetEndpointAddress(new java.net.URL(endpoint));
        //设置方法名
        call.setOperationName(new QName(namespace, methodName));

        //设置参数
        call.addParameter(new QName(namespace, "qqCode")
                , org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
        //设置返回类型
        call.setReturnType(XMLType.XSD_SCHEMA);
        //启用soap
        call.setUseSOAPAction(true);
        //设置soapAction
        call.setSOAPActionURI(soapAction);
        //设置服务名
        SOAPService soapService = new SOAPService();
        soapService.setName(serviceName);
        call.setSOAPService(soapService);
        Schema result = (Schema) call.invoke(new Object[]{"xxxxx"});
        for (int i = 0; i < result.get_any().length; i++) {
            System.out.println(result.get_any()[i]);
        }
    } catch (Exception e) {
        log.error("ddd", e);
    }
}
登入後複製

#對於以上程式碼,我這邊吐槽一下,網路上其實很多這個的例子但是實際呼叫的時候會出問題,注意點:

  • 設定參數

SpringBoot如何使用axis呼叫webservice介面

  • 取得結果

SpringBoot如何使用axis呼叫webservice介面

#xxxx需要填入真實的QQ號碼

以上是SpringBoot如何使用axis呼叫webservice介面的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:yisu.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!