Web Service简单demo
最近開發因需求要求需要提供Web Service接口供外部調用,由于之前沒有研究過該技術,故查閱資料研究了一番,所以寫下來記錄一下,方便后續使用。
這個demo采用CXF框架進行開發,后續所提到的Web Service 均由WS所替代。
一、CXF所使用的maven依賴,版本為:
<cxf.version>3.1.4</cxf.version> <dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-core</artifactId><version>${cxf.version}</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http</artifactId><version>${cxf.version}</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>${cxf.version}</version></dependency>
二、創建WS接口
import javax.jws.WebMethod; import javax.jws.WebService;@WebService public interface LogServiceWS {@WebMethodTSLog getLogById(String id); }?
三、實現類
@WebService public class LogServiceWSImpl implements LogServiceWS {@Autowiredprivate SystemService systemService;public LogServiceWSImpl(){System.out.println("LogServiceWSImpl 初始化了。。。。。。。");}@Overridepublic TSLog getLogById(String id) {return systemService.getEntity(TSLog.class, id);} }切記,實現類和接口盡量放在同一個包中,這樣可以避免后續生成的WSDL文件有import標簽,導致解析麻煩,或者在實現類上配置接口具體位置來解決該問題。
四、接下來配置CXF的配置文件cxf-beans.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/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsd"> <!-- Cxf WebService 服務端示例 --> <jaxws:endpoint id="userServiceWSImpl" implementor="com.svw.hrssc.webservice.ws.LogServiceWSImpl" address="/log/getLogById"/> </beans>?
implementor:表示WS接口的實現類
address:表示該接口的訪問地址
由于使用的CXF為3.0以上版本,所以不需要引入那三個配置文件
<import resource="classpath:META-INF/cxf/cxf.xml"/> <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/> <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
五、接下來配置web.xml將CXF加入到項目啟動容器中,項目啟動的時候發布WS接口。
首先把cxf-beans.xml文件加入context-param中,項目啟動的時候加載CXF配置文件,發布WS接口。
<context-param><param-name>contextConfigLocation</param-name><param-value>classpath*:spring-mvc-aop.xml,classpath*:spring-mvc.xml,classpath*:cxf-beans.xml</param-value></context-param>然后配置org.apache.cxf.transport.servlet.CXFServlet 作用:過濾請求,將符合CXF的請求交給接口處理。
<!--過濾cxf請求--><servlet><servlet-name>cxf</servlet-name><servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>cxf</servlet-name><url-pattern>/ws/services/*</url-pattern></servlet-mapping>根據配置可知,當有 /ws/services/* 格式的請求都會被過濾,然后交給CXF來處理。
至此,CXF服務端開發完成,可以啟動項目訪問:http://localhost:8080/sscmanage/ws/services/? ?查看接口是否發布完成。
點擊WSDL后面的鏈接,可以看到CXF產生的WSDL協議。標準的WSDL協議包含如下6部分:
六、測試客戶端開發
?
- 根據項目中所引用的cxf版本,自己去下載cxf開發包 apache-cxf-3.1.4.zip
- 解壓包至磁盤目錄,建議放到開發常用目錄
- 配置環境變量:在系統環境變量中創建? 環境變量,變量名:CXF_HOME? ?變量值:D:\software\development\apache-cxf-3.1.4? ?然后在系統環境變量Path下添加??%CXF_HOME%\bin? 即可,然后打開CMD窗口,輸入 wsdl2java -v? ?驗證是否正常
- WSDL:開發好接口后運行項目,訪問http://localhost:8081/sscmanage/ws/services/log/getLogById?wsdl可以看到所產生的的WSDL文檔,這是WS接口的標準規范,具體含義還請自行查資料。
- 然后通過WSDL去生成客戶端代碼;wsdl2java -d D:\\src -client?http://localhost:8081/sscmanage/ws/services/log/getLogById?wsdl? ?命令解析:?wsdl2java是cxf提供的一個根據WSDL文件生成客戶端的一個工具 ,因為配置了環境變量,所以可以直接調用,D:\\src? 表示在D盤下的src目錄下生成代碼,http://localhost:8081/sscmanage/ws/services/log/getLogById?wsdl?為運行項目后所產生的的WSDL文檔,表示根據該文檔生成對應的客戶端。
- 生成代碼后,將代碼拷貝到創建的java項目中,大致目錄如圖:
- 打開測試類可以看到
- 至此,WS開發demo完畢,項目中CXF的配置已經配置完成,只需要開發對應的接口、實現類和cxf-bean.xml文件即可,開發完成后要記得測試通過!!!!!
轉載于:https://www.cnblogs.com/blog411032/p/10534304.html
總結
以上是生活随笔為你收集整理的Web Service简单demo的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 多媒体具有的特征
- 下一篇: EEG中的EOG伪迹