Web Service简单demo
最近開發(fā)因需求要求需要提供Web Service接口供外部調(diào)用,由于之前沒有研究過該技術(shù),故查閱資料研究了一番,所以寫下來記錄一下,方便后續(xù)使用。
這個(gè)demo采用CXF框架進(jìn)行開發(fā),后續(xù)所提到的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>
二、創(chuàng)建WS接口
import javax.jws.WebMethod; import javax.jws.WebService;@WebService public interface LogServiceWS {@WebMethodTSLog getLogById(String id); }?
三、實(shí)現(xiàn)類
@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);} }切記,實(shí)現(xiàn)類和接口盡量放在同一個(gè)包中,這樣可以避免后續(xù)生成的WSDL文件有import標(biāo)簽,導(dǎo)致解析麻煩,或者在實(shí)現(xiàn)類上配置接口具體位置來解決該問題。
四、接下來配置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 服務(wù)端示例 --> <jaxws:endpoint id="userServiceWSImpl" implementor="com.svw.hrssc.webservice.ws.LogServiceWSImpl" address="/log/getLogById"/> </beans>?
implementor:表示W(wǎng)S接口的實(shí)現(xiàn)類
address:表示該接口的訪問地址
由于使用的CXF為3.0以上版本,所以不需要引入那三個(gè)配置文件
<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加入到項(xiàng)目啟動(dòng)容器中,項(xiàng)目啟動(dòng)的時(shí)候發(fā)布WS接口。
首先把cxf-beans.xml文件加入context-param中,項(xiàng)目啟動(dòng)的時(shí)候加載CXF配置文件,發(fā)布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 作用:過濾請(qǐng)求,將符合CXF的請(qǐng)求交給接口處理。
<!--過濾cxf請(qǐng)求--><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>根據(jù)配置可知,當(dāng)有 /ws/services/* 格式的請(qǐng)求都會(huì)被過濾,然后交給CXF來處理。
至此,CXF服務(wù)端開發(fā)完成,可以啟動(dòng)項(xiàng)目訪問:http://localhost:8080/sscmanage/ws/services/? ?查看接口是否發(fā)布完成。
點(diǎn)擊WSDL后面的鏈接,可以看到CXF產(chǎn)生的WSDL協(xié)議。標(biāo)準(zhǔn)的WSDL協(xié)議包含如下6部分:
六、測試客戶端開發(fā)
?
- 根據(jù)項(xiàng)目中所引用的cxf版本,自己去下載cxf開發(fā)包 apache-cxf-3.1.4.zip
- 解壓包至磁盤目錄,建議放到開發(fā)常用目錄
- 配置環(huán)境變量:在系統(tǒng)環(huán)境變量中創(chuàng)建? 環(huán)境變量,變量名:CXF_HOME? ?變量值:D:\software\development\apache-cxf-3.1.4? ?然后在系統(tǒng)環(huán)境變量Path下添加??%CXF_HOME%\bin? 即可,然后打開CMD窗口,輸入 wsdl2java -v? ?驗(yàn)證是否正常
- WSDL:開發(fā)好接口后運(yùn)行項(xiàng)目,訪問http://localhost:8081/sscmanage/ws/services/log/getLogById?wsdl可以看到所產(chǎn)生的的WSDL文檔,這是WS接口的標(biāo)準(zhǔn)規(guī)范,具體含義還請(qǐng)自行查資料。
- 然后通過WSDL去生成客戶端代碼;wsdl2java -d D:\\src -client?http://localhost:8081/sscmanage/ws/services/log/getLogById?wsdl? ?命令解析:?wsdl2java是cxf提供的一個(gè)根據(jù)WSDL文件生成客戶端的一個(gè)工具 ,因?yàn)榕渲昧谁h(huán)境變量,所以可以直接調(diào)用,D:\\src? 表示在D盤下的src目錄下生成代碼,http://localhost:8081/sscmanage/ws/services/log/getLogById?wsdl?為運(yùn)行項(xiàng)目后所產(chǎn)生的的WSDL文檔,表示根據(jù)該文檔生成對(duì)應(yīng)的客戶端。
- 生成代碼后,將代碼拷貝到創(chuàng)建的java項(xiàng)目中,大致目錄如圖:
- 打開測試類可以看到
- 至此,WS開發(fā)demo完畢,項(xiàng)目中CXF的配置已經(jīng)配置完成,只需要開發(fā)對(duì)應(yīng)的接口、實(shí)現(xiàn)類和cxf-bean.xml文件即可,開發(fā)完成后要記得測試通過!!!!!
轉(zhuǎn)載于:https://www.cnblogs.com/blog411032/p/10534304.html
總結(jié)
以上是生活随笔為你收集整理的Web Service简单demo的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 多媒体具有的特征
- 下一篇: bin文件读写 - C/C++