Java調用wsdl接口的兩種方法(axis和wsimport)
一、AXIS調用遠程WebService,以國內手機號歸屬地查詢?yōu)槔?nbsp;
1、wsdl地址:http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl
2、導入依賴:
使用axis遠程調用webService需要使用到axis、jaxrpc-api、commons-logging、commons-discovery等jar包。方便起見可以新建maven項目,在pom中導入依賴
<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、調用有入?yún)⒌膚ebservice接口
閱讀wsdl文件,我們可以了解方法名、參數(shù)和返回類型
<wsdl:operation name="getMobileCodeInfo"> <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"><br /><h3>獲得國內手機號碼歸屬地省份、地區(qū)和手機卡類型信息</h3><p>輸入?yún)?shù):mobileCode = 字符串(手機號碼,最少前7位數(shù)字),userID = 字符串(商業(yè)用戶ID) 免費用戶為空字符串;返回數(shù)據(jù):字符串(手機號碼:省份 城市 手機卡類型)。</p><br /></wsdl:documentation> <wsdl:input message="tns:getMobileCodeInfoSoapIn"/> <wsdl:output message="tns:getMobileCodeInfoSoapOut"/> </wsdl:operation>
方法名為:getMobileCodeInfo
參數(shù)有兩個:mobileCode手機號碼,字符串類型;userID用戶id,字符串類型(可以為空)
返回類型為字符串
Java調用代碼
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"); /** * 設置方法名 * 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[]{"手機號碼"}); System.out.println(result); }
4、調用無參的webservice接口
調用無參的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 /><h3>獲得國內手機號碼歸屬地數(shù)據(jù)庫信息</h3><p>輸入?yún)?shù):無;返回數(shù)據(jù):一維字符串數(shù)組(省份 城市 記錄數(shù)量)。</p><br /></wsdl:documentation> <wsdl:input message="tns:getDatabaseInfoSoapIn"/> <wsdl:output message="tns:getDatabaseInfoSoapOut"/> </wsdl:operation>
方法名:getDatabaseInfo,參數(shù):無,返回類型:一維字符串數(shù)組
Java調用代碼
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轉換為Java接口
wsimport命令是JDK自帶的命令,它能夠根據(jù)服務端說明書(wsdl)生成對應的本地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文件拷貝至原先項目中
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("手機號碼", null); System.out.println(mobileCodeInfo); List<String> dbInfo = soap.getDatabaseInfo().getString(); System.out.println(dbInfo); } }
wsimport生成的Java代碼中自定義了一個ArrayOfString數(shù)據(jù)結構,用于接收webservice返回的字符串數(shù)組,用getString()方法可以將之轉化為列表
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調用wsdl接口的兩種方法(axis和wsimport)的文章就介紹到這了,更多相關Java調用wsdl接口內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
mybatisplus中EntityWrapper的常用方法
這篇文章主要介紹了mybatisplus中EntityWrapper的常用方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03