我們項目的整個架構使用的比較流行的WSH MVC組合,即webwork2 + Spring + Hibernate;
1.首先集成Apacha CXF WebService 到 Spring 框架中;
?? apache cxf 下載地址:http://people.apache.org/dist/incubator/cxf/2.0.4-incubator/apache-cxf-2.0.4-incubator.zip
? 在spring context配置文件中引入以下cxf配置
?? Xml代碼
<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中添加過濾器:
?? Xml代碼

2.開發服務端WebService接口:
??? Java代碼
  1. /** ?
  2. ?*?WebService接口定義類. ?
  3. ?*? ?
  4. ?*?使用@WebService將接口中的所有方法輸出為Web?Service. ?
  5. ?*?可用annotation對設置方法、參數和返回值在WSDL中的定義. ?
  6. ?*/??
  7. @WebService??
  8. public?interface?WebServiceSample?{ ??
  9. ??
  10. ??
  11. ????/** ?
  12. ?????*?一個簡單的方法,返回一個字符串 ?
  13. ?????*?@param?hello ?
  14. ?????*?@return ?
  15. ?????*/??
  16. ????String?say(String?hello); ??
  17. ???? ??
  18. ????/** ?
  19. ?????*?稍微復雜一些的方法,傳遞一個對象給服務端處理 ?
  20. ?????*?@param?user ?
  21. ?????*?@return ?
  22. ?????*/??
  23. ????String?sayUserName( ??
  24. ????????????@WebParam(name?=?"user")? ??
  25. ????????????UserDTO?user); ??
  26. ???? ??
  27. ????/** ?
  28. ?????*?最復雜的方法,返回一個List封裝的對象集合 ?
  29. ?????*?@return ?
  30. ?????*/??
  31. ????public? ??
  32. ????@WebResult(partName="o") ??
  33. ????ListObject?findUsers(); ??
  34. ??
  35. }??

?? 由簡單到復雜定義了三個接口,模擬業務需求;

3.實現接口

?? Java代碼
  1. /** ?
  2. ?*?WebService實現類. ?
  3. ?*? ?
  4. ?*?使用@WebService指向Interface定義類即可. ?
  5. ?*/??
  6. @WebService(endpointInterface?=?"cn.org.coral.biz.examples.webservice.WebServiceSample") ??
  7. public?class?WebServiceSampleImpl?implements?WebServiceSample?{ ??
  8. ??
  9. ????public?String?sayUserName(UserDTO?user)?{ ??
  10. ????????return?"hello?"+user.getName(); ??
  11. ????} ??
  12. ??
  13. ????public?String?say(String?hello)?{ ??
  14. ????????return?"hello?"+hello; ??
  15. ????} ??
  16. ??
  17. ????public?ListObject?findUsers()?{ ??
  18. ????????ArrayList<Object>?list?=?new?ArrayList<Object>(); ??
  19. ???????? ??
  20. ????????list.add(instancUser(1,"lib")); ??
  21. ????????list.add(instancUser(2,"mld")); ??
  22. ????????list.add(instancUser(3,"lq")); ??
  23. ????????list.add(instancUser(4,"gj")); ??
  24. ????????ListObject?o?=?new?ListObject(); ??
  25. ????????o.setList(list); ??
  26. ????????return?o; ??
  27. ????} ??
  28. ???? ??
  29. ????private?UserDTO?instancUser(Integer?id,String?name){ ??
  30. ????????UserDTO?user?=?new?UserDTO(); ??
  31. ????????user.setId(id); ??
  32. ????????user.setName(name); ??
  33. ????????return?user; ??
  34. ????} ??
  35. }??

?4.依賴的兩個類:用戶對象與List對象
???? Java代碼

  1. /** ?
  2. ?*?Web?Service傳輸User信息的DTO. ?
  3. ?*? ?
  4. ?*?分離entity類與web?service接口間的耦合,隔絕entity類的修改對接口的影響. ?
  5. ?*?使用JAXB?2.0的annotation標注JAVA-XML映射,盡量使用默認約定. ?
  6. ?*? ?
  7. ?*/??
  8. @XmlAccessorType(XmlAccessType.FIELD) ??
  9. @XmlType(name?=?"User") ??
  10. public?class?UserDTO?{ ??
  11. ??
  12. ????protected?Integer?id; ??
  13. ??
  14. ????protected?String?name; ??
  15. ??
  16. ????public?Integer?getId()?{ ??
  17. ????????return?id; ??
  18. ????} ??
  19. ??
  20. ????public?void?setId(Integer?value)?{ ??
  21. ????????id?=?value; ??
  22. ????} ??
  23. ??
  24. ????public?String?getName()?{ ??
  25. ????????return?name; ??
  26. ????} ??
  27. ??
  28. ????public?void?setName(String?value)?{ ??
  29. ????????name?=?value; ??
  30. ????} ??
  31. }??

?? 關于List對象,參照了有關JWS的一個問題中的描述:DK6.0 自帶的WebService中 WebMethod的參數好像不能是ArrayList 或者其他List
傳遞List需要將List 包裝在其他對象內部才行 (個人理解 如有不對請指出) ,我在實踐中也遇到了此類問題.通過以下封裝的對象即可以傳遞List對象.

Java代碼
  1. /** ?
  2. ?*?<p>Java?class?for?listObject?complex?type. ?
  3. ?*? ?
  4. ?*?<p>The?following?schema?fragment?specifies?the?expected?content?contained?within?this?class. ?
  5. ?*? ?
  6. ?*?<pre> ?
  7. ?*?<complexType?name="listObject"> ?
  8. ?*???<complexContent> ?
  9. ?*?????<restriction?base="{http://www.w3.org/2001/XMLSchema}anyType"> ?
  10. ?*???????<sequence> ?
  11. ?*?????????<element?name="list"?type="{http://www.w3.org/2001/XMLSchema}anyType"?maxOccurs="unbounded"?minOccurs="0"/> ?
  12. ?*???????</sequence> ?
  13. ?*?????</restriction> ?
  14. ?*???</complexContent> ?
  15. ?*?</complexType> ?
  16. ?*?</pre> ?
  17. ?*? ?
  18. ?*? ?
  19. ?*/??
  20. @XmlAccessorType(XmlAccessType.FIELD) ??
  21. @XmlType(name?=?"listObject",?propOrder?=?{?"list"?}) ??
  22. public?class?ListObject?{ ??
  23. ??
  24. ????@XmlElement(nillable?=?true) ??
  25. ????protected?List<Object>?list; ??
  26. ??
  27. ????/** ?
  28. ?????*?Gets?the?value?of?the?list?property. ?
  29. ?????*? ?
  30. ?????*?<p> ?
  31. ?????*?This?accessor?method?returns?a?reference?to?the?live?list, ?
  32. ?????*?not?a?snapshot.?Therefore?any?modification?you?make?to?the ?
  33. ?????*?returned?list?will?be?present?inside?the?JAXB?object. ?
  34. ?????*?This?is?why?there?is?not?a?<CODE>set</CODE>?method?for?the?list?property. ?
  35. ?????*? ?
  36. ?????*?<p> ?
  37. ?????*?For?example,?to?add?a?new?item,?do?as?follows: ?
  38. ?????*?<pre> ?
  39. ?????*????getList().add(newItem); ?
  40. ?????*?</pre> ?
  41. ?????*? ?
  42. ?????*? ?
  43. ?????*?<p> ?
  44. ?????*?Objects?of?the?following?type(s)?are?allowed?in?the?list ?
  45. ?????*?{@link?Object?} ?
  46. ?????*? ?
  47. ?????*? ?
  48. ?????*/??
  49. ????public?List<Object>?getList()?{ ??
  50. ????????if?(list?==?null)?{ ??
  51. ????????????list?=?new?ArrayList<Object>(); ??
  52. ????????} ??
  53. ????????return?this.list; ??
  54. ????} ??
  55. ??
  56. ????public?void?setList(ArrayList<Object>?list)?{ ??
  57. ????????this.list?=?list; ??
  58. ????} ??
  59. ??
  60. }??

5.WebService 服務端 spring 配置文件 ws-context.xml
Xml代碼
  1. <beans?xmlns="http://www.springframework.org/schema/beans"??
  2. ????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"??
  3. ????xmlns:jaxws="http://cxf.apache.org/jaxws"??
  4. ????xsi:schemaLocation="http://cxf.apache.org/jaxws?http://cxf.apache.org/schemas/jaxws.xsd?http://www.springframework.org/schema/beans??http://www.springframework.org/schema/beans/spring-beans.xsd"??
  5. ????default-autowire="byName"?default-lazy-init="true">??
  6. ???? ??
  7. ????<jaxws:endpoint?id="webServiceSample"??
  8. ????????address="/WebServiceSample"?implementor="cn.org.coral.biz.examples.webservice.WebServiceSampleImpl"/>??
  9. ??
  10. </beans>??

WebService 客戶端 spring 配置文件 wsclient-context.xml

Xml代碼
  1. <beans?xmlns="http://www.springframework.org/schema/beans"??
  2. ????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"??
  3. ????xmlns:jaxws="http://cxf.apache.org/jaxws"??
  4. ????xsi:schemaLocation="http://cxf.apache.org/jaxws?http://cxf.apache.org/schemas/jaxws.xsd?http://www.springframework.org/schema/beans??http://www.springframework.org/schema/beans/spring-beans.xsd"??
  5. ????default-autowire="byName"?default-lazy-init="true">??
  6. ??
  7. ????<!--?ws?client?-->??
  8. ????<bean?id="identityValidateServiceClient"?class="cn.org.coral.admin.service.IdentityValidateService"??
  9. ????????factory-bean="identityValidateServiceClientFactory"?factory-method="create"?/>??
  10. ??
  11. ????<bean?id="identityValidateServiceClientFactory"??
  12. ????????class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">??
  13. ????????<property?name="serviceClass"??
  14. ????????????value="cn.org.coral.admin.service.IdentityValidateService"?/>??
  15. ????????<property?name="address"??
  16. ????????????value="http://88.148.29.54:8080/coral/services/IdentityValidateService"/>??
  17. ????</bean>??
  18. ???? ??
  19. </beans>??
6.發布到tomcat服務器以后通過以下地址即可查看自定義的webservice接口生成的wsdl:
http://88.148.29.54:8080/aio/services/WebServiceSample?wsdl

7.調用WebService接口的Junit單元測試程序
Java代碼
  1. package?test.coral.sample; ??
  2. ??
  3. import?org.springframework.test.AbstractDependencyInjectionSpringContextTests; ??
  4. ??
  5. import?cn.org.coral.biz.examples.webservice.WebServiceSample; ??
  6. import?cn.org.coral.biz.examples.webservice.dto.UserDTO; ??
  7. ??
  8. public?class?TestWebServiceSample?extends??
  9. ????????AbstractDependencyInjectionSpringContextTests?{ ??
  10. ????WebServiceSample?webServiceSampleClient; ??
  11. ??
  12. ????public?void?setWebServiceSampleClient(WebServiceSample?webServiceSampleClient)?{ ??
  13. ????????this.webServiceSampleClient?=?webServiceSampleClient; ??
  14. ????} ??
  15. ???? ??
  16. ????@Override??
  17. ????protected?String[]?getConfigLocations()?{ ??
  18. ????????setAutowireMode(AUTOWIRE_BY_NAME); ??
  19. ??????????????????//spring?客戶端配置文件保存位置 ??
  20. ????????return?new?String[]?{?"classpath:/cn/org/coral/biz/examples/webservice/wsclient-context.xml"?}; ??
  21. ????} ??
  22. ???? ??
  23. ????public?void?testWSClinet(){ ??
  24. ????????Assert.hasText(webServiceSampleClient.say("?world")); ??
  25. ????} ??
  26. }??