首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

WebService框架收拾(二) Axis1+Spring

2012-09-27 
WebService框架整理(二) Axis1+SpringWebService框架整理(二)?Axis1+Spring?(2010-11-18 16:13:06)转载▼标

WebService框架整理(二) Axis1+Spring

WebService框架整理(二)?Axis1+Spring?WebService框架收拾(二) Axis1+SpringWebService框架收拾(二) Axis1+Spring(2010-11-18 16:13:06)WebService框架收拾(二) Axis1+Spring转载标签:?

杂谈?文章分类:Java编程初识Axis1就要把它集成到Spring框架上。一方面是当时的项目要求,在一方面更是我对于Spring情有独钟。?
Axis1+Spring比较简单,这种便利得益于Spring的ServletEndpointSupport类支持。?

相关链接:?
WebService框架整理(一) Axis1?
WebService框架整理(二) Axis1+Spring?

我们将用到以下Jar:?
引用
activation.jar?
axis.jar?
commons-discovery.jar?
commons-logging.jar?
jaxrpc.jar?
log4j-1.2.15.jar?
mail.jar?
wsdl4j.jar?
spring.jar?

主要就是加入了spring.jar包!?

再看看web.xml,加入了Spring的相关内容。大家都熟悉Spring,我就不废话了!?
Xml代码??spring-axis-1 log4jConfigLocation classpath:log4j.xml log4jRefreshInterval 60000 contextConfigLocation /WEB-INF/applicationContext.xml UTF-8 Filter org.springframework.web.filter.CharacterEncodingFilter encoding UTF-8 forceEncoding true UTF-8 Filter public interface CalcService { int add(int a, int b); } " quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">
  1. ??
  2. public?interface?CalcService?{??
  3. ??
  4. ??????
  5. ????int?add(int?a,?int?b);??
  6. ??
  7. }??

给出对应的实现内容:?
Java代码?
  1. import?org.zlex.axis.service.CalcService;??
  2. ??
  3. ??
  4. public?class?CalcServiceImpl?implements?CalcService?{??
  5. ??
  6. ????@Override??
  7. ????public?int?add(int?a,?int?b)?{??
  8. ????????return?a?+?b;??
  9. ????}??
  10. ??
  11. }??

再简单不过的1+1问题!WebService框架收拾(二) Axis1+Spring?
将其注入到Spring的容器中,applicationContext.xml如下所示:?
Xml代码??" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">
  1. <?xml?version="1.0"?encoding="UTF-8"?>??
  2. <beans??
  3. ????xmlns="http://www.springframework.org/schema/beans"??
  4. ????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"??
  5. ????xmlns:context="http://www.springframework.org/schema/context"??
  6. ????xsi:schemaLocation="http://www.springframework.org/schema/beans?http://www.springframework.org/schema/beans/spring-beans.xsd??
  7. ????????http://www.springframework.org/schema/context?http://www.springframework.org/schema/context/spring-context.xsd">??
  8. ????<bean??
  9. ????????id="calcService"??
  10. ????????class="org.zlex.axis.service.impl.CalcServiceImpl"?/>??
  11. </beans>??

作为spring与axis1对接,需要做一个ServletEndpointSupport继承实现WebService,如下所示:?
Java代码?
  1. import?javax.xml.rpc.ServiceException;??
  2. ??
  3. import?org.springframework.context.ApplicationContext;??
  4. import?org.springframework.remoting.jaxrpc.ServletEndpointSupport;??
  5. import?org.zlex.axis.service.CalcService;??
  6. ??
  7. ??
  8. public?class?WebService?extends?ServletEndpointSupport?{??
  9. ??
  10. ????private?ApplicationContext?applicationContext;??
  11. ????private?CalcService?calcService;??
  12. ??
  13. ??????
  14. ????@Override??
  15. ????protected?void?onInit()?throws?ServiceException?{??
  16. ????????//?初始化Spirng?配置??
  17. ????????applicationContext?=?super.getApplicationContext();??
  18. ??
  19. ????????calcService?=?(CalcService)?applicationContext.getBean("calcService");??
  20. ??
  21. ????}??
  22. ??
  23. ??????
  24. ????public?String?add(int?a,?int?b)?{??
  25. ????????return?String.valueOf(calcService.add(a,?b));??
  26. ????}??
  27. ??
  28. }??

这里为了便于在eclipse演示,将返回值定为String类型!?
现在我们将该服务植入Axis中,修改server-config.wsdd文件,在原文件中加入如下内容:?
Xml代码??" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">
  1. <!--?自定义服务?-->??
  2. <service??
  3. ????name="WebService"??
  4. ????provider="java:RPC">??
  5. ????<parameter??
  6. ????????name="className"??
  7. ????????value="org.zlex.axis.WebService"?/>??
  8. </service>??

修改后的server-config.wsdd文件如下所示:?
Xml代码??http://xml.apache.org/axis/wsdd/ " quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">
  1. <?xml?version="1.0"?encoding="UTF-8"?>??
  2. <deployment??
  3. ????xmlns="http://xml.apache.org/axis/wsdd/"??
  4. ????xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">??
  5. ????<globalConfiguration>??
  6. ????????<parameter??
  7. ????????????name="adminPassword"??
  8. ????????????value="admin"?/>??
  9. ????????<parameter??
  10. ????????????name="sendXsiTypes"??
  11. ????????????value="true"?/>??
  12. ????????<parameter??
  13. ????????????name="sendMultiRefs"??
  14. ????????????value="true"?/>??
  15. ????????<parameter??
  16. ????????????name="sendXMLDeclaration"??
  17. ????????????value="true"?/>??
  18. ????????<parameter??
  19. ????????????name="axis.sendMinimizedElements"??
  20. ????????????value="true"?/>??
  21. ????????<requestFlow>??
  22. ????????????<handler??
  23. ????????????????type="java:org.apache.axis.handlers.JWSHandler">??
  24. ????????????????<parameter??
  25. ????????????????????name="scope"??
  26. ????????????????????value="session"?/>??
  27. ????????????</handler>??
  28. ????????????<handler??
  29. ????????????????type="java:org.apache.axis.handlers.JWSHandler">??
  30. ????????????????<parameter??
  31. ????????????????????name="scope"??
  32. ????????????????????value="request"?/>??
  33. ????????????????<parameter??
  34. ????????????????????name="extension"??
  35. ????????????????????value=".jwr"?/>??
  36. ????????????</handler>??
  37. ????????</requestFlow>??
  38. ????</globalConfiguration>??
  39. ????<handler??
  40. ????????name="Authenticate"??
  41. ????????type="java:org.apache.axis.handlers.SimpleAuthenticationHandler"?/>??
  42. ????<handler??
  43. ????????name="LocalResponder"??
  44. ????????type="java:org.apache.axis.transport.local.LocalResponder"?/>??
  45. ????<handler??
  46. ????????name="URLMapper"??
  47. ????????type="java:org.apache.axis.handlers.http.URLMapper"?/>??
  48. ????<service??
  49. ????????name="AdminService"??
  50. ????????provider="java:MSG">??
  51. ????????<parameter??
  52. ????????????name="allowedMethods"??
  53. ????????????value="AdminService"?/>??
  54. ????????<parameter??
  55. ????????????name="enableRemoteAdmin"??
  56. ????????????value="false"?/>??
  57. ????????<parameter??
  58. ????????????name="className"??
  59. ????????????value="org.apache.axis.utils.Admin"?/>??
  60. ????????<namespace>http://xml.apache.org/axis/wsdd/</namespace>??
  61. ????</service>??
  62. ????<service??
  63. ????????name="Version"??
  64. ????????provider="java:RPC">??
  65. ????????<parameter??
  66. ????????????name="allowedMethods"??
  67. ????????????value="getVersion"?/>??
  68. ????????<parameter??
  69. ????????????name="className"??
  70. ????????????value="org.apache.axis.Version"?/>??
  71. ????</service>??
  72. ????<transport??
  73. ????????name="http">??
  74. ????????<requestFlow>??
  75. ????????????<handler??
  76. ????????????????type="URLMapper"?/>??
  77. ????????????<handler??
  78. ????????????????type="java:org.apache.axis.handlers.http.HTTPAuthHandler"?/>??
  79. ????????</requestFlow>??
  80. ????</transport>??
  81. ????<transport??
  82. ????????name="local">??
  83. ????????<responseFlow>??
  84. ????????????<handler??
  85. ????????????????type="LocalResponder"?/>??
  86. ????????</responseFlow>??
  87. ????</transport>??
  88. ??
  89. ????<!--?自定义服务?-->??
  90. ????<service??
  91. ????????name="WebService"??
  92. ????????provider="java:RPC">??
  93. ????????<parameter??
  94. ????????????name="className"??
  95. ????????????value="org.zlex.axis.WebService"?/>??
  96. ????</service>??
  97. </deployment>??

我们随机抽取2个数进行求和运算,并验证WebService和本地计算结果是否一致,测试用例WebServiceTest如下:?

热点排行