使用CXF開(kāi)發(fā)WebServi_第1頁(yè)
使用CXF開(kāi)發(fā)WebServi_第2頁(yè)
使用CXF開(kāi)發(fā)WebServi_第3頁(yè)
使用CXF開(kāi)發(fā)WebServi_第4頁(yè)
使用CXF開(kāi)發(fā)WebServi_第5頁(yè)
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、文檔從網(wǎng)絡(luò)中收集,己重新整理排版.word版本可編輯,歡迎下載支持.使用 Spring+CXF 開(kāi)發(fā) WebServiceApache CXF提供方便的Spring整合方法,可以通過(guò)注解、Spring標(biāo)簽式配置來(lái)暴露 Web Services 和消費(fèi) Web Services各種類型的 Annotationo WebService WebMethod 是 WSDL 映射 Annatotiono 這 些Annotation將描述 Web Service的 WSDL文檔元素和Java源代碼聯(lián)系在一起。 SOAPBinding是一個(gè)綁定的annotation用來(lái)說(shuō)明網(wǎng)絡(luò)協(xié)議和格式。1 WebServ

2、ice annotation 的元素 nanie,serviceNan】e 和 targetNaniespace 成員用來(lái)描述 wsdl:portTypet wsdl:service,和 targetNameSpace 生成 WebService 中的 WSDL 文件。2、SOAPBinding是一個(gè)用來(lái)描述SOAP格式和RPC的協(xié)議的綁定Annotationa3、WebMethod Annotation 的 operationName 成員描述了 wsdLoperation,而且它的操作描 述了 WSDL文檔中的SOAPAction頭部。這是客戶端必須要放入到SQAPHeader中的數(shù) 值,

3、SOAP 1.1中的一種約束。4、WebParam Annotation 的 partName 成員描述了 WSDL 文檔中的 wsdlpart。5、WebResult Annotation 的 partName 成員描述了 wsdl:part 用來(lái)返回 WSDL 文檔的值。例如下而使用annotation定義了一個(gè)webservice:import j ava util.List;import j avax.jws.WebMethod;import j avax . j vzs . WebParam;import j avax . jvzs . WebResult;import javax.

4、jws.WebService;import com.cxf poj o.User;WebService(targetNamespace = nhttp:/j dk study.hermit , org/client11) public interface UserService WebMethod (operationName=M Insert1)public void insert ( WebParam(name = userid) String userid,?7ebParam (name =G?7ebParam (name =HuserNameH) String username,nus

5、erEmailn) String useremail,G?7ebParam (name =“userAge) int userage);WebMethod (operationName=,GetUserByIdH) WebResult(name = result)public User getUserByld(QWebParam(name=HuseridH) String userid);WebMethod(operationName=MGetAllUsersM) QWebResult (name = resultH)public List getAllUsers();其實(shí)現(xiàn)類如下所示:imp

6、ort java.util.List;import javax.jws.WebService; import com.cxf.dao UserDao;import com.cxf poj o.User; import com.cxf service UserService; WebService (endpoint I nterface=,*001x1. cxf . service . UserService*1) public class UserServicelmpl implements UserService private UserDao userDao;public List ge

7、tAllUsers() return userDao.findAllUser(); )public User getUserByld(String userid) return userDao.findUserById(userid); public void insert(String useridr String username, String useremail, int userage) User user=new User ();user setUserage(userage);user setUseremail(useremail);user.setUserid(userid);

8、user setUsername(username);userDao.insert(user);System.out.printin(Hinsert successfully!n); public void setUserDao(UserDao userDao) this.userDao = userDao; ) 注意:實(shí)現(xiàn)類中的WebService,其中的endpointinterface成員指定了該類實(shí)現(xiàn)的接口在Spring的配置文件中,需要對(duì)其進(jìn)行配置:首先在Applicationcontext.xml(Spring的配置文件)中引入CXF的XML Scheam配置文件), 如下:imp

9、ort resource=Mclasspath:META-INF/cxf/cxf.xmlH /import resource=Hclasspath:META-INF/cxf/exf-extension-soap xmln/import resource=Hclasspath:META-INF/cxf/cxf-servlet.xmlH /其次就是在Spring的配置文件中配置webservice如下所示: 注意:、address為webservWce發(fā)布的地址、implementorClass 為該實(shí)現(xiàn)的接口、/ 3 a:-:ws :Implementor之 I向定義的是 impl emen t

10、or Cl ass 指定接口的實(shí)現(xiàn)類另外,在Spring的配置文件中配置完成后,需要修改web.xm1文件 CXFServletorg apache , exf transport.servlet.CXFServletCXFServlet/services/*注意/services/*配置,CXFServlet 會(huì)攔截此類 uii,進(jìn)行處理。上 而配置的webservice將通過(guò)如下URL訪問(wèn)到:http:/工ocalhost: 8080/cxf-ws/services/ UserManager UserManager 為:ws :qndpoint 標(biāo)簽中 address 屬性對(duì)應(yīng)的值 cxf

11、-v;s為本項(xiàng)目的名稱配置完成之后,將項(xiàng)目部署到tomcat上輸入 URL: http: /localhost: 8080/cxf-ws/services/ UserManager?wsdl 將會(huì)看到生成的wsdl文件為了能夠直接訪問(wèn)到com.cxf . service . UserService中的insert方法,進(jìn)行如下測(cè)試: 創(chuàng)建,個(gè) insertuser .html 頁(yè)面: function post () var xmlhttp=false;if (!xmlhttp & typeof XMLHttpRequest! = 1 undefined1) try xmlhttp = new

12、 XMLHttpRequest(); catch (e) xmlhttp=false;if (!xmlhttp & window.createRequest) try (xmlhttp = window.createRequest(); catch (e) xmlhttp=false;var dest = document.getElementByld(r destination r) .value;xmlhttp.open(HPOSTH, dest, true);xmlhttp.onreadystatechange=function() if (xmlhttp.readyState=4) d

13、ocument getElementByld(resultH.innerHTML = xmlhttp , responseText; xmlhttp.setRequestHeader(HManH, HPOST H + dest + ” HTTP/1.1M) xmlhttp.setRequestHeader(nMessageTypeH, CALL)xmlhttp . setRequestHeader (,Content-TypeH , Mtext/xmln );var texta = document.getElementByld(1texta1);var soapmess = texta.va

14、lue; xmlhttp.send(soapmess);hi there2005221104210066Leon Caocaohongliang223Result:在該測(cè)試頁(yè)面需要注意標(biāo)記為紅色的部分,如下是一個(gè)soap message:2005221104210066Leon Cao23注意:SOAP-ENV: E =二:標(biāo)簽之間二二=七二對(duì)應(yīng)WebMethod (operationName=HInsert,f) 中的operationName屬性的值2_ Id% % 標(biāo)簽對(duì)各自對(duì)應(yīng)著insert方法中中name屬性的值:WebParam(name = userid) String userid,WebParam(name = userNameH) String username,Web?aram(name = HuserEmailn) String useremail,Web?aram(name = HuserAgeH) int userag

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論