您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“Java調(diào)用wsdl接口的方法有哪些”的有關(guān)知識,在實(shí)際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
1、wsdl地址:http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl
2、導(dǎo)入依賴:
使用axis遠(yuǎn)程調(diào)用webService需要使用到axis、jaxrpc-api、commons-logging、commons-discovery等jar包。方便起見可以新建maven項(xiàng)目,在pom中導(dǎo)入依賴
<dependencies> <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.5</version> <exclusions> <exclusion> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </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.6.3</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.11.0</version> </dependency> </dependencies>
3、調(diào)用有入?yún)⒌膚ebservice接口
閱讀wsdl文件,我們可以了解方法名、參數(shù)和返回類型
<wsdl:operation name="getMobileCodeInfo"> <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"><br /><h4>獲得國內(nèi)手機(jī)號碼歸屬地省份、地區(qū)和手機(jī)卡類型信息</h4><p>輸入?yún)?shù):mobileCode = 字符串(手機(jī)號碼,最少前7位數(shù)字),userID = 字符串(商業(yè)用戶ID) 免費(fèi)用戶為空字符串;返回數(shù)據(jù):字符串(手機(jī)號碼:省份 城市 手機(jī)卡類型)。</p><br /></wsdl:documentation> <wsdl:input message="tns:getMobileCodeInfoSoapIn"/> <wsdl:output message="tns:getMobileCodeInfoSoapOut"/> </wsdl:operation>
方法名為:getMobileCodeInfo
參數(shù)有兩個:mobileCode手機(jī)號碼,字符串類型;userID用戶id,字符串類型(可以為空)
返回類型為字符串
Java調(diào)用代碼
public static void getMobileCodeInfo() throws ServiceException, RemoteException { Service service = new Service(); Call call = (Call) service.createCall(); // wsdl完整地址 call.setTargetEndpointAddress("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl"); /** * 設(shè)置方法名 * new QName(String namespaceURI, String localPart) namespaceURI即為wsdl中的targetNamespace, localPart即為接口名 */ call.setOperationName(new QName("http://WebXml.com.cn/", "getMobileCodeInfo")); /** * 添加參數(shù) * addParameter方法的參數(shù)包括:參數(shù)名(namespace+參數(shù)名)、參數(shù)類型、ParameterMode(入?yún)⒓礊镮N) */ call.addParameter(new QName("http://WebXml.com.cn/", "mobileCode"), XMLType.XSD_STRING, ParameterMode.IN); call.setUseSOAPAction(true); // SOAPActionURI格式為targetNamespace+方法名 call.setSOAPActionURI("http://WebXml.com.cn/getMobileCodeInfo"); // 指定返回值類型,為字符串 call.setReturnType(XMLType.XSD_STRING); call.setReturnClass(java.lang.String.class); String result = (String) call.invoke(new Object[]{"手機(jī)號碼"}); System.out.println(result); }
4、調(diào)用無參的webservice接口
調(diào)用無參的webservice接口無需添加參數(shù),并且在invoke方法中傳入的是一個空的對象數(shù)組
T result = (T)call.invoke(new Object[]{});
閱讀wsdl文件,了解方法名、參數(shù)和返回類型
<wsdl:operation name="getDatabaseInfo"> <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"><br /><h4>獲得國內(nèi)手機(jī)號碼歸屬地數(shù)據(jù)庫信息</h4><p>輸入?yún)?shù):無;返回數(shù)據(jù):一維字符串?dāng)?shù)組(省份 城市 記錄數(shù)量)。</p><br /></wsdl:documentation> <wsdl:input message="tns:getDatabaseInfoSoapIn"/> <wsdl:output message="tns:getDatabaseInfoSoapOut"/> </wsdl:operation>
方法名:getDatabaseInfo,參數(shù):無,返回類型:一維字符串?dāng)?shù)組
Java調(diào)用代碼
public static void getDatabaseInfo() throws ServiceException, RemoteException { Service service = new Service(); Call call = (Call) service.createCall(); call.setTargetEndpointAddress("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl"); //?wsdl call.setOperationName(new QName("http://WebXml.com.cn/", "getDatabaseInfo")); call.setUseSOAPAction(true); call.setSOAPActionURI("http://WebXml.com.cn/getDatabaseInfo"); call.setReturnType(XMLType.XSD_UNSIGNEDBYTE); call.setReturnClass(java.lang.String[].class); String[] returnContext = (String[]) call.invoke(new Object[]{}); for (String s : returnContext) { System.out.println(s); } }
wsimport命令是JDK自帶的命令,它能夠根據(jù)服務(wù)端說明書(wsdl)生成對應(yīng)的本地java代碼。這種方法相較于第一種要簡單很多,不用閱讀wsdl文件。
wsimport -d <生成.class文件的目錄> -s <生成.java文件的目錄> -p<包名> <wsdl地址>
在D:\wsdl下新建文件夾class用于存放.class文件,文件夾java用于存放.java文件
D:\wsdl>wsimport -d class -s java -p mobileCode http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl
執(zhí)行命令,生成java代碼
將Java文件拷貝至原先項(xiàng)目中
Java代碼示例
import mobileCode.MobileCodeWS; import mobileCode.MobileCodeWSSoap; import java.util.List; public class Main { public static void main(String[] args) { MobileCodeWS mobileCodeWS = new MobileCodeWS(); MobileCodeWSSoap soap = mobileCodeWS.getMobileCodeWSSoap(); String mobileCodeInfo = soap.getMobileCodeInfo("手機(jī)號碼", null); System.out.println(mobileCodeInfo); List<String> dbInfo = soap.getDatabaseInfo().getString(); System.out.println(dbInfo); } }
wsimport生成的Java代碼中自定義了一個ArrayOfString數(shù)據(jù)結(jié)構(gòu),用于接收webservice返回的字符串?dāng)?shù)組,用getString()方法可以將之轉(zhuǎn)化為列表
package mobileCode; import java.util.ArrayList; import java.util.List; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlType; @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "ArrayOfString", propOrder = { "string" }) public class ArrayOfString { @XmlElement(nillable = true) protected List<String> string; public List<String> getString() { if (string == null) { string = new ArrayList<String>(); } return this.string; } }
“Java調(diào)用wsdl接口的方法有哪些”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。