目錄
一、idea安裝webservice
1.點(diǎn)擊左上file,選中settings?編輯
2.下載Web Service
3.給此項(xiàng)目添加webservice
4.添加webservice的依賴(lài)
二、利用idea根據(jù)wsdl文件自動(dòng)生成webService客戶(hù)端代碼(然后比照著生成的測(cè)試類(lèi)進(jìn)行接口或方法的調(diào)用)
1.打開(kāi)tools -> WebServices -> Generate Java Code From Wsdl,按照?qǐng)D中順序進(jìn)行
2.按照?qǐng)D中順序進(jìn)行操作,最后點(diǎn)擊OK
3.必須勾選Generate TestCase
4.wsdl文件轉(zhuǎn)換生成Java代碼:成功
5.如何使用生成的代碼
三、直接利用Axis2調(diào)用wsdl類(lèi)型接口:
一、idea安裝webservice
1.點(diǎn)擊左上file,選中settings
2.下載Web Service
3.給此項(xiàng)目添加webservice
?
4.添加webservice的依賴(lài)
依賴(lài)加就行鏡像用阿里云的基本都能下載
<!-- axis 1.4 jar start -->
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-discovery</groupId>
<artifactId>commons-discovery</artifactId>
<version>0.2</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis-jaxrpc</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis-saaj</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.4</version>
</dependency>
???<!--webservice的:TestCase需要的jar包--> ????
? ? ? ? <dependency>
????????????<groupId>junit</groupId>
????????????<artifactId>junit</artifactId>
????????????<version>4.12</version>
????????</dependency>
二、利用idea根據(jù)wsdl文件自動(dòng)生成webService客戶(hù)端代碼(然后比照著生成的測(cè)試類(lèi)進(jìn)行接口或方法的調(diào)用)
步驟:1.利用idea根據(jù)wsdl文件生成代碼,需要先將wsdl文件下載到本地
? ? 2.下載時(shí)無(wú)必要要求,選擇Apache Axis,依賴(lài)需要自行百度搜索
????3.下載后需要比照著測(cè)試類(lèi)代碼,寫(xiě)調(diào)用接口或方法的代碼
好處:1.入?yún)⒌膮?shù)不需要自己進(jìn)行封裝 , 特別容易寫(xiě)
?????2.返回值都被封裝好了 , 可以很容易就取到 , 不用自己寫(xiě)解析代碼
1.打開(kāi)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所需的依賴(lài) , 第一次需額外下載一個(gè)插件(直接下載完就行)
只要不是報(bào)wsdl文件出錯(cuò) , 其他報(bào)錯(cuò)后,只要能生成代碼 , 就可以使用
4.wsdl文件轉(zhuǎn)換生成Java代碼:成功
例子1:
??
?例子2:
5.如何使用生成的代碼
點(diǎn)擊打開(kāi)名字中帶Test的這個(gè)類(lèi),找到你需要的方法,直接復(fù)制出來(lái)就能用
三、直接利用Axis2調(diào)用wsdl類(lèi)型接口:
好處:不用利用idea根據(jù)wsdl自動(dòng)生成webService客戶(hù)端代碼,然后再比照著測(cè)試類(lèi)調(diào)用所需方法
壞處: 1.入?yún)⑿枰约哼M(jìn)行參數(shù)的封裝 ,自己封裝參數(shù)有例子也不容易
2.如果需要獲取調(diào)用接口的返回值,則還不會(huì)解析返回的數(shù)據(jù),無(wú)法得到所需的參數(shù)(沒(méi)解析報(bào)文的例子,自己不會(huì)寫(xiě))文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-717298.html
Axis2?調(diào)用接口示例:文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-717298.html
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) { } } } }
到了這里,關(guān)于利用idea生成webservice客戶(hù)端--詳解步驟--(wsdl文件的使用)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!