1、下載Apache CXF包,配置環境變量
下載鏈接:http://cxf.apache.org/download.html
在path環境變量中,加入bin目錄,G:\tool\apache-cxf-3.3.5\bin,
驗證環境變量是否配置成功,在dos窗口,輸入wsdl2java 即可。
2、生成客戶端代碼
1、常用到的命令:
wsdl2java -h 可以得到詳細的參考文檔
-p 指定cxf生成代碼的包名;例如 希望在項目中的com.java.service.send包中生成代碼,則在命令中加入 -p com.java.service.send ,代碼就會生成在項目的send包中。該命令可以避免生成的代碼中包名與項目包衝突的情況。
-d 指定cxf生成代碼的目錄;生成的代碼,會在指定的目錄 + -p 指定的包名 中。
-client 是否生成客戶端測試webservice的代碼;會生成main方法測試代碼,可以參照這個類寫調用代碼。
-encoding 指定編碼格式,如 UTF-8;
以下爲了解命令:
-server 生成服務器啓動webservice 的代碼;
-impl 生成webservice的實現代碼;
-keep 保留源文件;
-s <dir> 指定源代碼存放目錄;
-autoNameResolution 當報異常:具有相同名稱 "xxxx" 的類/接口已在使用。請使用類定製設置來解決此衝突。
是因爲 wsdl 文檔中有重複的元素導致的,在命令中加此參數,可以解決類名衝突的問題。
-ant 生成build.xml文件;
-verbose 指定生成器以詳細模式運行;
-all 生成所有開始端點代碼;
-version 顯示版本;
2、wsdl文件準備:
① 服務端 url,例如 http://168.1.x.x:8090/service-epr/services/epr?wsdl
② wsdl文件,例如 xxxx.wsdl 不常用。可以訪問wsdl接口,下載頁面內容,去除無用的接口定義,否則會生成不需要的對象。
3、新建java工程,引入所需依賴:
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-core</artifactId>
<version>3.1.9</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.9</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.9</version>
</dependency>
在applicationContext.xml 添加命名路徑和標籤(這裏新創建了一個配置文件applicationContext-cxf.xml,方便配置):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
</beans>
4、生成客戶端代碼:
在dos窗口,輸入 wsdl2java -p com.abc.service.send -d G:\workspace\ws_client\src\main\java -client http://168.1.x.x:8090/service-epr/services/epr?wsdl
回車,完成。使用 wsdl 文件生成代碼,與使用 url 生成方法一致。
也可以cd進到G:\workspace\ws_client\src\main\java目錄下,wsdl2java -p com.abc.service.send -client http://168.1.x.x:8090/service-epr/services/epr?wsdl
5、生成客戶端代碼後,可以按生成的客戶端測試代碼調用服務端接口,或者自己寫:
TestServiceIService ss= new TestServiceIService();
TestServiceI service = ss.getTestServiceIPort();
//添加調用接口所需參數
Person p = new Person();
p.setName("張三");
Response res = service.getInfo(p);
System.out.println("返回的結果是" + res.getCode() +" " + res.getMsg());
6、補充:
服務端需要添加頭信息(驗證信息)時,可在服務端接口的參數上指定添加@WebParam(name = "token", header = true),如:
Response getInfo(@WebParam(name = "person") Person person,@WebParam(name = "token", header = true) String token,@WebParam(name = "idCode", header = true) String idCode,@WebParam(name = "requesttype", header = true) String requestType);
//此接口生成的wsdl
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:edis="http://eprService.factory.com/">
<soapenv:Header>
<edis:requesttype>?</edis:requesttype>
<edis:idCode>?</edis:idCode>
<edis:token>?</edis:token>
</soapenv:Header>
<soapenv:Body>
<edis:getInfo>
<!--Optional:-->
<person>
<!--Optional:-->
<name>?</name>
</person>
</edis:getInfo>
</soapenv:Body>
</soapenv:Envelope>
客戶端使用 wsdl2java 生成代碼後,調用服務端接口方法時,直接set參數就可以完成頭信息的添加。
* 使用soapUI也可以生成客戶端代碼,以後學習後補充。
參考資源:https://blog.csdn.net/panshoujia/article/details/78899160