使用CXF做簡單的WebService例子
阿新 • • 發佈:2018-04-24
str 實例 分享 ora 路徑 run inf 調用 version
使用Maven搭建項目測試簡單的CXF實例
Server:
pom.xml:
<!-- begin CXF Server --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.1.1</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>3.1.1</version> </dependency> <dependency> <!-- 如果CXF不集成到Web服務器中,必須添加該引用 --> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-jetty</artifactId> <version>3.1.1</version> </dependency> <!-- End CXF Server -->
定義WebServer訪問接口: ICXFService
package com.cxf; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; @WebService(name= "cxfService", targetNamespace = "http://localhost/services/testCXF") public interface ICXFService { @WebMethod String test1(@WebParam(name = "name")String name); }
定義接口的具體實現:CXFServiceImpl
package com.cxf.impl; import javax.jws.WebService; import com.cxf.ICXFService; @WebService(endpointInterface = "com.cxf.ICXFService", portName = "HelloCXF", serviceName = "HelloCXFService", targetNamespace = "http://localhost/services/testCXF") public class CXFServiceImpl implements ICXFService { @Override public String test1(String name) { return "Hello " + name; } }
測試服務:
package com.cxf; import org.apache.cxf.jaxws.JaxWsServerFactoryBean; import com.cxf.impl.CXFServiceImpl; public class CXFServiceRun { public static void main(String[] args) { JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean(); factory.setServiceClass(ICXFService.class); factory.setAddress("http://localhost:8888/services/hello"); factory.setServiceBean(new CXFServiceImpl()); factory.create(); } }
訪問: http://localhost:8888/services/hello?wsdl
Client:
創建CXF的客戶端maven工程
添加 CXF 必須的 jar :
<!-- begin CXF Client --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.1.1</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>3.1.1</version> </dependency> <!-- End CXF Client -->
下載 apache 的 CXF 安裝包 本次使用的是 apache-cxf-3.1.15 解壓,配置環境變量就可以使用。
配置環境變量:
配置完成之後再 命令窗口 下執行 wsdl2java -help 出現如下信息表示配置成功:
執行:wsdl2java -encoding UTF-8 -d C:\Users\豐誌\Desktop\cxfServer\src\main\java -p com.cxf.generate http://localhost:8888/services/hello?wsdl
-d 後面跟生成java代碼的目錄, -p後面跟生成代碼的包名稱,最後跟wsdl的鏈接地址(或wsdl文件路徑 + 文件名稱)
生成的代碼目錄文件:
然後將CXF生成的代碼粘貼到客戶端項目中(也可以直接將代碼生成到eclipse中的webService的客戶端工程中)
編寫客戶端測試:TestClient
package com.cxf.client; import java.net.MalformedURLException; import java.net.URL; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; import com.cxf.generate.CxfService; import com.cxf.generate.ICXFServiceService; public class TestClient { public static void main(String[] args) { //jaxws調用 // URL不是必須的,除非服務的地址有改變 // URL wsdlUrl = null; // try { // wsdlUrl = new URL("http://localhost:8888/services/hello?wsdl"); // } catch (MalformedURLException e) { // e.printStackTrace(); // } // ICXFServiceService factory = new ICXFServiceService(wsdlUrl); //// ICXFServiceService factory = new ICXFServiceService(); // CxfService cxfService = factory.getCxfServicePort(); // String name = cxfService.test1("lisi"); // System.out.println(name); // CXF 調用 JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); factory.setServiceClass(CxfService.class); factory.setAddress("http://localhost:8888/services/hello"); CxfService cxfService = factory.create(CxfService.class); String name = cxfService.test1("lisi"); System.out.println(name); } }
兩種實現方式都可以
運行成功:
源碼:https://files.cnblogs.com/files/guofz/FirstCXF.rar
參考:https://blog.csdn.net/accountwcx/article/details/47082487
使用CXF做簡單的WebService例子