Java調(diào)用wsdl接口的兩種方法(axis和wsimport)
一、AXIS調(diào)用遠(yuǎn)程WebService,以國(guó)內(nèi)手機(jī)號(hào)歸屬地查詢?yōu)槔?nbsp;
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 /><h3>獲得國(guó)內(nèi)手機(jī)號(hào)碼歸屬地省份、地區(qū)和手機(jī)卡類型信息</h3><p>輸入?yún)?shù):mobileCode = 字符串(手機(jī)號(hào)碼,最少前7位數(shù)字),userID = 字符串(商業(yè)用戶ID) 免費(fèi)用戶為空字符串;返回?cái)?shù)據(jù):字符串(手機(jī)號(hào)碼:省份 城市 手機(jī)卡類型)。</p><br /></wsdl:documentation> <wsdl:input message="tns:getMobileCodeInfoSoapIn"/> <wsdl:output message="tns:getMobileCodeInfoSoapOut"/> </wsdl:operation>
方法名為:getMobileCodeInfo
參數(shù)有兩個(gè):mobileCode手機(jī)號(hào)碼,字符串類型;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ī)號(hào)碼"}); System.out.println(result); }
4、調(diào)用無參的webservice接口
調(diào)用無參的webservice接口無需添加參數(shù),并且在invoke方法中傳入的是一個(gè)空的對(duì)象數(shù)組
T result = (T)call.invoke(new Object[]{});
閱讀wsdl文件,了解方法名、參數(shù)和返回類型
<wsdl:operation name="getDatabaseInfo"> <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"><br /><h3>獲得國(guó)內(nèi)手機(jī)號(hào)碼歸屬地?cái)?shù)據(jù)庫(kù)信息</h3><p>輸入?yún)?shù):無;返回?cái)?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方法將wsdl轉(zhuǎn)換為Java接口
wsimport命令是JDK自帶的命令,它能夠根據(jù)服務(wù)端說明書(wsdl)生成對(duì)應(yīng)的本地java代碼。這種方法相較于第一種要簡(jiǎn)單很多,不用閱讀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ī)號(hào)碼", null); System.out.println(mobileCodeInfo); List<String> dbInfo = soap.getDatabaseInfo().getString(); System.out.println(dbInfo); } }
wsimport生成的Java代碼中自定義了一個(gè)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; } }
到此這篇關(guān)于Java調(diào)用wsdl接口的兩種方法(axis和wsimport)的文章就介紹到這了,更多相關(guān)Java調(diào)用wsdl接口內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章

mybatisplus中EntityWrapper的常用方法

java實(shí)現(xiàn)統(tǒng)一異常處理的示例

Java數(shù)據(jù)結(jié)構(gòu)順序表用法詳解

java對(duì)象和json的來回轉(zhuǎn)換知識(shí)點(diǎn)總結(jié)

Java內(nèi)存溢出實(shí)現(xiàn)原因及解決方案