通過CXF提供的wsdl2java工具生成客戶端代碼,生成的客戶端代碼中帶有測試工具類,可以直接使用進行代碼調試,方法類似於jdk自帶的wsipmort命令。
使用的前提是需要下載CXF jar文件,本文以apache-cxf-3.1.3版本進行客戶端代碼生成。
1. wsdl2java 環境配置(使用命令前必須的環境變量配置)
CXF_HOME = D:\apache-cxf-3.1.3
CLASSPATH = %CXF_HOME%\lib
PATH = %CXF_HOME%\bin
2. 驗證配置是否成功(打印CXF版本就說明配置OK)
3. 使用命令生成客戶端代碼
命令解釋:
-d: 指定代碼生成到本地的路徑,“webClient”文件夾需提前創建好
-p: 指定客戶端代碼生成的包號(com.cxf.service.webclient)
-client:指定服務器wsdl地址
4. 將生成的客戶端代碼複製到項目中
5. IBaseWebService_BaseWebServiceImpPort_Client爲測試工具類
package com.cxf.service.webclient;
/**
* Please modify this class to meet your needs
* This class is not complete
*/
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.jws.WebMethod;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;
/**
* This class was generated by Apache CXF 3.1.3
* 2015-10-27T17:09:33.167+08:00
* Generated source version: 3.1.3
*
*/
public final class IBaseWebService_BaseWebServiceImpPort_Client {
private static final QName SERVICE_NAME = new QName("http://imp.service.cxf.com/", "Hello");
private IBaseWebService_BaseWebServiceImpPort_Client() {
}
public static void main(String args[]) throws java.lang.Exception {
URL wsdlURL = Hello.WSDL_LOCATION;
if (args.length > 0 && args[0] != null && !"".equals(args[0])) {
File wsdlFile = new File(args[0]);
try {
if (wsdlFile.exists()) {
wsdlURL = wsdlFile.toURI().toURL();
} else {
wsdlURL = new URL(args[0]);
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
Hello ss = new Hello(wsdlURL, SERVICE_NAME);
IBaseWebService port = ss.getBaseWebServiceImpPort();
{
System.out.println("Invoking helloWorld...");
java.lang.String _helloWorld__return = port.helloWorld();
System.out.println("helloWorld.result=" + _helloWorld__return);
}
System.exit(0);
}
}
6. 運行得到測試結果
源代碼下載地址:http://download.csdn.net/detail/onepersontz/9217885