利用idea生成webservice客戶端超詳解步驟(wsdl文件的使用)
一、idea安裝webservice
1.點(diǎn)擊左上file,選中settings
2.下載Web Service
3.給此項(xiàng)目添加webservice
4.添加webservice的依賴
<!--webservice的:TestCase需要的jar包--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency>
二、利用idea根據(jù)wsdl文件自動(dòng)生成webService客戶端代碼(然后比照著生成的測(cè)試類進(jìn)行接口或方法的調(diào)用)
步驟:1.利用idea根據(jù)wsdl文件生成代碼,需要先將wsdl文件下載到本地
2.下載時(shí)無必要要求,選擇Apache Axis,依賴需要自行百度搜索
3.下載后需要比照著測(cè)試類代碼,寫調(diào)用接口或方法的代碼
好處:1.入?yún)⒌膮?shù)不需要自己進(jìn)行封裝 , 特別容易寫
2.返回值都被封裝好了 , 可以很容易就取到 , 不用自己寫解析代碼
1.打開tools -> WebServices -> Generate Java Code From Wsdl,按照?qǐng)D中順序進(jìn)行
2.按照?qǐng)D中順序進(jìn)行操作,最后點(diǎn)擊OK
3.必須勾選Generate TestCase
(會(huì)給出調(diào)用接口的例子,照著抄就可以實(shí)現(xiàn)調(diào)用指定接口)
4.2點(diǎn)擊ok報(bào)錯(cuò) , 就去百度下載Axis所需的依賴 , 第一次需額外下載一個(gè)插件(直接下載完就行)
只要不是報(bào)wsdl文件出錯(cuò) , 其他報(bào)錯(cuò)后,只要能生成代碼 , 就可以使用
4.wsdl文件轉(zhuǎn)換生成Java代碼:成功
例子1:
例子2:
5.如何使用生成的代碼
點(diǎn)擊打開名字中帶Test的這個(gè)類,找到你需要的方法,直接復(fù)制出來就能用
三、直接利用Axis2調(diào)用wsdl類型接口:
好處:不用利用idea根據(jù)wsdl自動(dòng)生成webService客戶端代碼,然后再比照著測(cè)試類調(diào)用所需方法
壞處: 1.入?yún)⑿枰约哼M(jìn)行參數(shù)的封裝 ,自己封裝參數(shù)有例子也不容易
2.如果需要獲取調(diào)用接口的返回值,則還不會(huì)解析返回的數(shù)據(jù),無法得到所需的參數(shù)(沒解析報(bào)文的例子,自己不會(huì)寫)
Axis2 調(diào)用接口示例:
import org.apache.axiom.om.OMAbstractFactory; import org.apache.axiom.om.OMElement; import org.apache.axiom.om.OMFactory; import org.apache.axiom.om.OMNamespace; import org.apache.axis2.AxisFault; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.rpc.client.RPCServiceClient; public class Test { public static void test() { String userId = "123"; String bindAccount = "123"; RPCServiceClient serviceClient = null; OMFactory factory = OMAbstractFactory.getOMFactory(); OMNamespace omDiag = factory.createOMNamespace("http://diagnosis.interfaces.axis2.osf.nort hbound.neal.cpehg.ums.zte.com", "diag"); OMNamespace omXSD = factory.createOMNamespace("http://model.common.northbound.neal.cpehg. ums.zte.com/xsd", "xsd"); try { serviceClient = new RPCServiceClient(); Options options = serviceClient.getOptions(); // 指定調(diào)用WebService的URL EndpointReference targetEPR = new EndpointReference("http://10.46.60.200:9094/axis2/services/Cpe112Diag nosisWebServices?wsdl"); options.setTo(targetEPR); options.setTimeOutInMilliSeconds(30000); options.setManageSession(true); // 指定方法的參數(shù)值 OMElement paramRequest = factory.createOMElement("request", omDiag); OMElement paramUserId = factory.createOMElement("userID", omXSD); paramUserId.setText(userId); OMElement paramBindAccount = factory.createOMElement("bindAccount", omXSD); paramBindAccount.setText(bindAccount); paramRequest.addChild(paramBindAccount); paramRequest.addChild(paramUserId); OMElement paramItemName = factory.createOMElement("itemName", omDiag); paramItemName.setText("cpehg.diagnosis.CpeBasicInfo"); String method = "getParameterValuesFromDbAndCpeByItemName"; OMElement data = factory.createOMElement(method, omXSD); data.setNamespace(omDiag); data.addChild(paramRequest); data.addChild(paramItemName); OMElement re = serviceClient.sendReceive(data); // 處理返回?cái)?shù)據(jù) } catch (AxisFault e) { // 異常處理 e.printStackTrace(); } finally { try { if (serviceClient != null) serviceClient.cleanupTransport(); } catch (AxisFault e) { } } } }
總結(jié)
到此這篇關(guān)于利用idea生成webservice客戶端的文章就介紹到這了,更多相關(guān)idea生成webservice客戶端內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- idea生成WebServices接口的完整流程步驟
- idea沒有services窗口、沒有springboot啟動(dòng)項(xiàng)問題
- idea如何在service窗口中顯示多個(gè)服務(wù)
- idea使用mybatis插件mapper中的方法爆紅的解決方案
- IDEA中@Autowired自動(dòng)注入MyBatis?Mapper報(bào)紅警告的幾種解決方法
- idea中springboot整合mybatis找不到mapper接口的原因分析
- idea注入mapper報(bào)錯(cuò)報(bào)紅的幾種解決方案
- idea中service或者mapper引入報(bào)紅的問題及解決
相關(guān)文章
Spring Cloud Gateway組件的三種使用方式實(shí)例詳解
Spring Cloud Gateway是 Spring 官方基于 Spring5.0 、 SpringBoot2.0 和 Project Reactor 等技術(shù)開發(fā)的網(wǎng)關(guān)旨在為微服務(wù)框架提供一種簡(jiǎn)單而有效的統(tǒng)一的API 路由管理方式,統(tǒng)一訪問接口,這篇文章主要介紹了Spring Cloud Gateway組件的三種使用方式,需要的朋友可以參考下2024-01-01java使用動(dòng)態(tài)代理來實(shí)現(xiàn)AOP(日志記錄)的實(shí)例代碼
AOP(面向方面)的思想,就是把項(xiàng)目共同的那部分功能分離開來,比如日志記錄,避免在業(yè)務(wù)邏輯里面夾雜著跟業(yè)務(wù)邏輯無關(guān)的代碼2013-09-09如何在springboot中配置和使用mybatis-plus
這篇文章主要給大家介紹了關(guān)于如何在springboot中配置和使用mybatis-plus的相關(guān)資料,MyBatis?Plus是MyBatis的增強(qiáng)版,旨在提供更多便捷的特性,減少開發(fā)工作,同時(shí)保留了MyBatis的靈活性和強(qiáng)大性能,需要的朋友可以參考下2023-11-11Hibernate懶加載之<class>標(biāo)簽上的lazy
這篇文章主要介紹了Hibernate懶加載之<class>標(biāo)簽上的lazy,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02SpringBoot使用Swagger生成多模塊的API文檔
這篇文章將以?Spring?Boot?多模塊項(xiàng)目為例,為大家詳細(xì)介紹一下如何使用?Swagger?生成多模塊的?API?文檔,感興趣的小伙伴可以了解一下2025-02-02