結(jié)合Spring框架的 CXF WebService編程實(shí)例參考模板_第1頁
結(jié)合Spring框架的 CXF WebService編程實(shí)例參考模板_第2頁
結(jié)合Spring框架的 CXF WebService編程實(shí)例參考模板_第3頁
結(jié)合Spring框架的 CXF WebService編程實(shí)例參考模板_第4頁
結(jié)合Spring框架的 CXF WebService編程實(shí)例參考模板_第5頁
已閱讀5頁,還剩9頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、結(jié)合Spring框架的 CXF WebService編程實(shí)例1. 需要的jar包,除了以上的jar包以外,還需要Spring包,這里就不一一列出了,根據(jù)項(xiàng)目的不同可能還需要其他的jar包,后期調(diào)試的時(shí)候根據(jù)控制臺(tái)出現(xiàn)的問題,找出原因。2. Web.xml的配置: org.springframework.web.context.ContextLoaderListener contextConfigLocation classpath*:applicationContext-server.xml org.springframework.web.util.IntrospectorCleanupLis

2、tener CXFService org.apache.cxf.transport.servlet.CXFServlet1 / 14 CXFService /*然后在src目錄中,新建一個(gè)applicationContext-server.xml文件,這個(gè)applicationContext-server.xml需要在web.xml文件中引入(仔細(xì)查看上面在web.xml中的配置),當(dāng)然不一定非要新建applicationContext-server.xml,也可以寫在applicationContext.xml中.如果applicationContext-server.xml沒有寫在src下

3、面則web.xml中的文件應(yīng)該這樣寫contextConfigLocation/WEB-INF/classes/config/spring/*.xml(這里解釋下:自己查看下編譯后的applicationContext-server.xml的位置是在項(xiàng)目)。我這是在src下面新建了config和spring兩個(gè)包,再在里面新建的applicationContext-server.xml(我這里夠啰嗦的了,但是為了你們明白點(diǎn),別整暈了啊,哈哈哈)。下面是applicationContext-server.xml中的內(nèi)容: 好了以上就是webservice服務(wù)器端外圍的webservice環(huán)境;下

4、面介紹具體的編碼實(shí)例;Javabean文件:里面有3個(gè)屬:編號(hào),姓名,電話package com.service;public class Person private String code;private String name;private String tel;public String getName() return name;public String getCode() return code;public void setCode(String code) this.code = code;public void setName(String name)

5、 = name;public String getTel() return tel;public void setTel(String tel) this.tel = tel;Webservice方法的接口,里面定義了兩個(gè)方法package com.service;import javax.jws.WebParam;import javax.jws.WebService;import javax.jws.soap.SOAPBinding;import javax.jws.soap.SOAPBinding.Style;WebService(name=PersonService)SOAPBindi

6、ng(style = Style.RPC)public interface PersonService /根據(jù)輸入的字符串對(duì)數(shù)據(jù)庫中的人員進(jìn)行模糊查詢WebParam(name=inputsth) String inputsthpublic String getPersonList(WebParam(name=inputsth) String inputsth);/根據(jù)登錄的Code,獲得人員信息public String getPersonInfo(WebParam(name=PROP_EMP_DISTINCT_NO) String PROP_EMP_DISTINCT_NO);以下是具體兩個(gè)

7、方法的實(shí)現(xiàn)類:(這里數(shù)據(jù)訪問采用的hibernate,dao層在這就不細(xì)講了)package com.service.impl;import java.util.ArrayList;import java.util.List;import javax.jws.WebMethod;import javax.jws.WebService;import org.hibernate.criterion.DetachedCriteria;import org.hibernate.criterion.Restrictions;import file.dao.IEmployeeDAO;i

8、mport file.domain.Employee;import com.service.CharacterSetToolkit;import com.service.Person;import com.service.PersonService;import com.service.User;WebServicepublic class PersonServiceImpl implements PersonServicepublic IEmployeeDAO getEmployeeDAO() return employeeDAO;public void setEmplo

9、yeeDAO(IEmployeeDAO employeeDAO) this.employeeDAO = employeeDAO;private IEmployeeDAO employeeDAO = null; WebMethodpublic String getPersonList(String inputsth) /inputsth=張; String s = CharacterSetToolkit.fromUnicode(inputsth.toCharArray(), 0, inputsth.length(), inputsth.toCharArray();System.out.print

10、ln(fffffffffffgg=+s); DetachedCriteria dc = DetachedCriteria.forClass(Employee.class); dc.add(Restrictions.ilike(Employee.PROP_EMP_NAME, %+s+%); List employee=employeeDAO.findByCriteria(dc); System.out.println(對(duì)象=+employee.size();if(employee!=null&employee.size()0)String name=;List pl = new ArrayLis

11、t();for(int i=0;iemployee.size();i+)name=employee.get(i).getEmpName();String code=employee.get(i).getEmpDistinctNo();String tel=employee.get(i).getEmpWorkPhone();System.out.println(111sss=+name);Person p=new Person();p.setName(name);p.setCode(code);p.setTel(tel);pl.add(p);/ return pl;return new com.

12、service.DataSyncXml().personListXml(pl);/ List list = new ArrayList();/ list.add(new Person();/return list;return null; WebMethodpublic String getPersonInfo(String PROP_EMP_DISTINCT_NO) DetachedCriteria dc = DetachedCriteria.forClass(Employee.class);dc.add(Restrictions.eq(Employee.PROP_EMP_DISTINCT_

13、NO, PROP_EMP_DISTINCT_NO);List employee= employeeDAO.findByCriteria(dc);if(employee!=null&employee.size()0) String name=employee.get(0).getEmpName(); String tel=employee.get(0).getEmpWorkPhone(); Person p=new Person(); p.setName(name); p.setTel(tel); System.out.println(1111=+p.getName()+2222=+p.getT

14、el(); return new com.service.DataSyncXml().personInfoXml(p);return null;注意以上CharacterSetToolkit這個(gè)方法,主要是當(dāng)時(shí)傳中文參數(shù)的時(shí)候,服務(wù)器端為亂碼,當(dāng)時(shí)花費(fèi)了我好大的勁才解決的,先將中文轉(zhuǎn)換成unicode編碼,再將unicode編碼換成中文就ok了package com.service;public class CharacterSetToolkit /* Creates a new instance of CharacterSetToolkit */ public CharacterSetToo

15、lkit() private static final char hexDigit = 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F ; private static char toHex(int nibble) return hexDigit(nibble & 0xF); /* * 將字符串編碼成 Unicode 。 * param theString 待轉(zhuǎn)換成Unicode編碼的字符串。 * param escapeSpace 是否忽略空格。 * return 返回轉(zhuǎn)換后Unicode編碼的字符串。 */ public static String toUnicode(St

16、ring theString, boolean escapeSpace) int len = theString.length(); int bufLen = len * 2; if (bufLen 0) bufLen = Integer.MAX_VALUE; StringBuffer outBuffer = new StringBuffer(bufLen); for(int x=0; x 61) & (aChar 127) if (aChar = ) outBuffer.append(); outBuffer.append(); continue; outBuffer.append(aCha

17、r); continue; switch(aChar) case : if (x = 0 | escapeSpace) outBuffer.append(); outBuffer.append( ); break; case t:outBuffer.append(); outBuffer.append(t); break; case n:outBuffer.append(); outBuffer.append(n); break; case r:outBuffer.append(); outBuffer.append(r); break; case f:outBuffer.append();

18、outBuffer.append(f); break; case =: / Fall through case : / Fall through case #: / Fall through case !: outBuffer.append(); outBuffer.append(aChar); break; default: if (aChar 0x007e) outBuffer.append(); outBuffer.append(u); outBuffer.append(toHex(aChar 12) & 0xF); outBuffer.append(toHex(aChar 8) & 0

19、xF); outBuffer.append(toHex(aChar 4) & 0xF); outBuffer.append(toHex( aChar & 0xF); else outBuffer.append(aChar); return outBuffer.toString(); /* * 從 Unicode 碼轉(zhuǎn)換成編碼前的特殊字符串。 * param in Unicode編碼的字符數(shù)組。 * param off 轉(zhuǎn)換的起始偏移量。 * param len 轉(zhuǎn)換的字符長度。 * param convtBuf 轉(zhuǎn)換的緩存字符數(shù)組。 * return 完成轉(zhuǎn)換,返回編碼前的特殊字符串。 */

20、public static String fromUnicode(char in, int off, int len, char convtBuf) if (convtBuf.length len) int newLen = len * 2; if (newLen 0) newLen = Integer.MAX_VALUE; convtBuf = new charnewLen; char aChar; char out = convtBuf; int outLen = 0; int end = off + len; while (off end) aChar = inoff+; if (aCh

21、ar = ) aChar = inoff+; if (aChar = u) / Read the xxxx int value = 0; for (int i = 0; i 4; i+) aChar = inoff+; switch (aChar) case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: value = (value 4) + aChar - 0; break; case a: case b: case c: case d: case e: case f: value = (

22、value 4) + 10 + aChar - a; break; case A: case B: case C: case D: case E: case F: value = (value 4) + 10 + aChar - A; break; default: throw new IllegalArgumentException( Malformed uxxxx encoding.); outoutLen+ = (char) value; else if (aChar = t) aChar = t; else if (aChar = r) aChar = r; else if (aCha

23、r = n) aChar = n; else if (aChar = f) aChar = f; outoutLen+ = aChar; else outoutLen+ = (char) aChar; return new String(out, 0, outLen); 最后將返回的東西,封裝成xml就行package com.service;import java.util.List;import org.dom4j.Document;import org.dom4j.DocumentHelper;import org.dom4j.Element;public class DataSyncX

24、ml /模糊查詢返回人員列表public String personListXml(List pl)Document doc = DocumentHelper.createDocument();Element personList = doc.addElement(PersonList);for(int i=0;ipl.size();i+)Person person = (Person)pl.get(i);Element p = personList.addElement(Person);p.addElement(NAME).addText(object2String(person.getNa

25、me();p.addElement(CODE).addText(object2String(person.getCode();p.addElement(TEL).addText(object2String(person.getTel();return doc.asXML(); /返回人員信息public String personInfoXml(Person p)Document doc = DocumentHelper.createDocument();Element personInfo = doc.addElement(PersonInfo);Element person = perso

26、nInfo.addElement(Person);person.addElement(NAME).addText(object2String(p.getName();person.addElement(TEL).addText(object2String(p.getTel();return doc.asXML();private String object2String(Object o)return o=null?:o.toString();怎樣在服務(wù)器端進(jìn)行webservice的測(cè)試呢,首先在瀏覽器的地址欄中輸入webservice的地址http:/192.168.5.xxx:8080/x

27、xx/service/PersonService?wsdl如果出現(xiàn)你所學(xué)的方法名稱,這說明你的webservice外圍環(huán)境搭建成功,現(xiàn)在看一下你寫的返回xml是否正確(我這是以返回對(duì)象0bject測(cè)試的)一下是我寫的mian方法,public class SpringUsersWsClient public static void main(String args) throws UnsupportedEncodingException 通過登錄的用戶名(工號(hào))查詢用戶的姓名和辦公電話 JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryB

28、ean(); factory.setServiceClass(PersonService.class); factory.setAddress(http:/192.168.xxx.xxx:8080/xxx/service/PersonService); PersonService service = (PersonService) factory.create(); Person p=service.getPersonInfo(TH505); String name=p.getName(); String tel=p.getTel(); System.out.println(人員姓名=+nam

29、e+辦公電話=+tel); JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); factory.setServiceClass(PersonService.class); factory.setAddress(http:/192.xxx.xxx.xxx:8080/xxx/service/PersonService); PersonService service = (PersonService) factory.create(); String inputsth=張; String uname =new String( in

30、putsth.getBytes(Charset.forName(GB2312) ); String uname=CharacterSetToolkit.toUnicode(inputsth, true); System.out.println(uname=+uname); List personlist=service.getPersonList(uname); System.out.println(人員姓名總量22222=+personlist.size(); for(int i=0;ipersonlist.size();i+) System.out.println(人員姓名2222222=

31、+personlist.get(i).getName(); 在客戶端調(diào)用剛剛所寫的webservicepublic class SpringWsClient /返回人員列表public static List getPersonName(String inputsth)throws UnsupportedEncodingException, ServiceException, MalformedURLException, RemoteException, DocumentException ClassLoader cl = Thread.currentThread().getContextCl

32、assLoader(); DynamicClientFactory dcf = DynamicClientFactory.newInstance(); Client client = dcf.createClient(http:/192.168.5.xxx:8080/xxx/service/PersonService?wsdl); Thread.currentThread().setContextClassLoader(cl); Object reply = null; /String inputsth=張; String uname=CharacterSetToolkit.toUnicode

33、(inputsth, true); try reply =client.invoke(getPersonList, uname); catch (Exception e) e.printStackTrace(); String result=(String) reply0;/ System.out.println(2222222www=+result); Document document = DocumentHelper.parseText(result); List listObject = document.selectNodes(/PersonList/Person); List pl

34、 = new ArrayList(); for (int i = 0; i listObject.size(); i+) Element personElement = (Element) listObject.get(i); String name = personElement.element(NAME).getText(); String code = personElement.element(CODE).getText(); String tel = personElement.element(TEL).getText(); /System.out.println(人員姓名=+name); Person p=new Person(); p.setName(name); p.setCode(code); p.setTel(tel); pl.add(p); return pl;/返回人員信息public static Person getPersonInfo(St

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(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)論