首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

步履艰难的XFire配置过程

2012-11-17 
步履维艰的XFire配置过程1、拷贝xfire相关的jar到classpath;2、在web.xml配置xfire的servlet及其url映射xml

步履维艰的XFire配置过程
1、拷贝xfire相关的jar到classpath;
2、在web.xml配置xfire的servlet及其url映射
xml 代码?

  1. <servlet>??
  2. ????<servlet-name>xfire<!---->servlet-name>??
  3. ????<servlet-class>org.springframework.web.servlet.DispatcherServlet<!---->servlet-class>??
  4. ????<init-param>??
  5. ????????<param-name>contextConfigLocation<!---->param-name>??
  6. ????????<param-value>classpath:xfire-servelet.xml<!---->param-value>??
  7. ????<!---->init-param>??
  8. <!---->servlet>??
3、新建xfire-servelet.xml
xml 代码?
  1. xml?version="1.0"?encoding="UTF-8"?>??
  2. >??
  3. <beans>??
  4. ????<import?resource="classpath:org/codehaus/xfire/spring/xfire.xml"/>??
  5. ????<bean?class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">??
  6. ????????<property?name="urlMap">??
  7. ????????????<map>??
  8. ????????????????<entry?key="/SAService">??
  9. ????????????????????<ref?bean="SAServiceWS"?/>??
  10. ????????????????<!---->entry>??
  11. ????????????????<entry?key="/BulletinService">??
  12. ????????????????????<ref?bean="BulletinServiceWS"?/>??
  13. ????????????????<!---->entry>??
  14. ????????????<!---->map>??
  15. ????????<!---->property>??
  16. ????<!---->bean>??
  17. ??
  18. ????<bean?id="SAServiceWS"?class="org.codehaus.xfire.spring.remoting.XFireExporter">??
  19. ????????<property?name="serviceFactory">??
  20. ????????????<ref?bean="xfire.serviceFactory"?/>??
  21. ????????<!---->property>??
  22. ????????<property?name="xfire">??
  23. ????????????<ref?bean="xfire"?/>??
  24. ????????<!---->property>??
  25. ????????<property?name="serviceBean">??
  26. ????????????<ref?bean="SAService"?/>??
  27. ????????<!---->property>??
  28. ????????<property?name="serviceClass">??
  29. ????????????<value>net.gbicc.sa.service.SAService<!---->value>??
  30. ????????<!---->property>??
  31. ????<!---->bean>??
  32. ??????
  33. ????<bean?id="BulletinServiceWS"?class="org.codehaus.xfire.spring.remoting.XFireExporter">??
  34. ????????<property?name="serviceFactory">??
  35. ????????????<ref?bean="xfire.serviceFactory"?/>??
  36. ????????<!---->property>??
  37. ????????<property?name="xfire">??
  38. ????????????<ref?bean="xfire"?/>??
  39. ????????<!---->property>??
  40. ????????<property?name="serviceBean">??
  41. ????????????<ref?bean="bulletinService"?/>??
  42. ????????<!---->property>??
  43. ????????<property?name="serviceClass">??
  44. ????????????<value>net.gbicc.swm.service.BulletinService<!---->value>??
  45. ????????<!---->property>??
  46. ????<!---->bean>??
  47. <!---->beans>??
4、至此,算是把spring组件发布成webservice了,运气好的话访问http://localhost:8880/swm/services/SAService?wsdl可以看到xfire为我们生成的漂亮而复杂的WSDL。
5、可是,一般情况下不会这么顺利的(参考http://wiki.springside.org.cn/display/springside/XFire的2.3),我们还需要为那些返回值为集合类型的方法以及包含集合属性的model做一些特殊配置:
在SAService.java同一目录新建SAService.aegis.xml
xml 代码?
  1. xml?version="1.0"?encoding="utf-8"?>??
  2. <mappings>??
  3. ????<mapping>??
  4. ????????<method?name="getPrivileges">??
  5. ????????????<return-type?componentType="net.gbicc.sa.domain.Privilege"?/>??
  6. ????????<!---->method>??
  7. ??
  8. ????????<method?name="getDepts">??
  9. ????????????<return-type?componentType="net.gbicc.sa.domain.Department"?/>??
  10. ????????<!---->method>??
  11. ??
  12. ????????<method?name="getRoles">??
  13. ????????????<return-type?componentType="net.gbicc.sa.domain.Role"?/>??
  14. ????????<!---->method>??
  15. ??
  16. ????????<method?name="getUsers">??
  17. ????????????<return-type?componentType="net.gbicc.sa.domain.User"?/>??
  18. ????????<!---->method>??
  19. ??
  20. ????????<method?name="getUsersByDept">??
  21. ????????????<return-type?componentType="net.gbicc.sa.domain.User"?/>??
  22. ????????<!---->method>??
  23. ??
  24. ????????<method?name="getUsersByRole">??
  25. ????????????<return-type?componentType="net.gbicc.sa.domain.User"?/>??
  26. ????????<!---->method>??
  27. ????<!---->mapping>??
  28. <!---->mappings>??
在User.java同一目录新建User.aegis.xml
xml 代码?
  1. xml?version="1.0"?encoding="utf-8"?>??
  2. <mappings>??
  3. ????<mapping>??
  4. ????????<property?name="roles"?ignore="true"?/>??
  5. ????<!---->mapping>??
  6. <!---->mappings>??
6、再次启动服务,如果运气不是特别差的话,访问http://localhost:8880/swm/services/SAService?wsdl可以看到xfire为我们生成的漂亮而复杂的WSDL了。
7、可是,能够生成WSDL并不意味着程序已经可以访问这个Webservice,还要用单元测试来验证一下:
java 代码?
  1. public?class?WSTest?extends?AbstractXFireSpringTest?{??
  2. ????private?Log?log?=?LogFactory.getLog(WSTest.class);??
  3. ??
  4. ????protected?ApplicationContext?createContext()?{??
  5. ????????return?new?ClassPathXmlApplicationContext(new?String[]?{?"classpath*:applicationContext*.xml",?"classpath*:xfire-servelet.xml"?});??
  6. ????}??
  7. ??
  8. ????public?void?testSAWS()?throws?Exception?{??
  9. ????????Service?serviceModel?=?new?ObjectServiceFactory().create(SAService.class);??
  10. ????????XFireProxyFactory?factory?=?new?XFireProxyFactory(getXFire());??
  11. ????????SAService?service?=?(SAService)?factory.create(serviceModel,?"xfire.local://SAService");??
  12. ????????List?list?=?service.getPrivileges();??
  13. ????????assertNotNull(list);??
  14. ????}??
  15. }??

因为继承了xfire提供的测试基类AbstractXFireSpringTest,使得我们可以不起服务就能测这个webservice。
8、如果运气不太差,应该可以看见绿条庆祝胜利了,可是,我却遇到了一个奇怪错误:org.codehaus.xfire.fault.XFireFault: No write method for property {http://domain.swm.gbicc.net}especialPrompt in class net.gbicc.swm.domain.Bulletin
而事实上Bulletin里并没有especialPrompt属性。。。。这令我很困惑,一度怀疑是Model继承导致的
后来终于找到症结所在——Bulletin里面的一些boolean isxxx方法把xfire骗了,为他们新建一些对应的setxxx(boolean)就没问题了,绿条如约出现。

整个过程虽然有些艰难,但是其实配置量还是很少的,再看看xfire替我们生成的一大堆WSDL,也就释然了,谁教咱程序员以“懒惰”为美德呢?

热点排行