当前位置: 代码迷 >> Web前端 >> Axis1.4+Spring的webservice兑现
  详细解决方案

Axis1.4+Spring的webservice兑现

热度:303   发布时间:2012-10-12 10:17:04.0
Axis1.4+Spring的webservice实现

最近用到了cxf的webservice组件,为外部系统提供的接口访问,对接过程中发现外部系统发过来的接口参数,在我这边竟然都是null,于是抓包分析,发现cxf产生的soap报文的格式是这样的:

Java代码 ?收藏代码
  1. <soap:Envelope?xmlns:soap= "http://schemas.xmlsoap.org/soap/envelope/" >??
  2. ????<soap:Body>??
  3. ????????<ns1:testMethod?xmlns:ns1="http://test/" >??
  4. ????????????<arg0>2006001 </arg0>??
  5. ????????????<arg1>admin</arg1>??
  6. ????????????<arg2>admin</arg2>??
  7. ????????????<arg3>beijing</arg3>??
  8. ????????</ns1:testMethod>??
  9. ????</soap:Body>??
  10. </soap:Envelope>??


而外部系统发过来的soap报文格式如下:

Java代码 ?收藏代码
  1. <?xml?version= "1.0" ?encoding= "UTF-8" ?>??
  2. <soapenv:Envelope?xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" ??
  3. ????xmlns:xsd="http://www.w3.org/2001/XMLSchema" ?xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" >??
  4. ????<soapenv:Body>??
  5. ????????<ns1:testMethod?soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" ??
  6. ????????????xmlns:ns1="http://soap.account.bossIntf.tjb.runway.com" >??
  7. ????????????<in0?xsi:type="xsd:string" > 2006001 </in0>??
  8. ????????????<in1?xsi:type="xsd:string" >admin</in1>??
  9. ????????????<in2?xsi:type="xsd:string" >admin</in2>??
  10. ????????????<in3?xsi:type="xsd:string" >beijing</in3>??
  11. ????????</ns1:testMethod?>??
  12. ????</soapenv:Body>??
  13. </soapenv:Envelope>??


于是询问外部系统公司人员,使用的是什么组件实现webservice,回答是axis1.4.

接着就是把接口都移植到axis组件上,axis结合spring,其实也就是获取spring的bean,首先在web.xml中配置axis的servlet:

Java代码 ?收藏代码
  1. <servlet>????
  2. ????????<servlet-name>axis</servlet-name>????
  3. ????????<servlet-class >????
  4. ????????????org.apache.axis.transport.http.AxisServlet?????
  5. ????????</servlet-class >????
  6. ????????<load-on-startup>3 </load-on-startup>??
  7. ????</servlet>????
  8. ????<servlet-mapping>????
  9. ????????<servlet-name>axis</servlet-name>????
  10. ????????<url-pattern>/servlet/AxisServlet</url-pattern>????
  11. ????</servlet-mapping>????
  12. ????<servlet-mapping>????
  13. ????????<servlet-name>axis</servlet-name>????
  14. ????????<url-pattern>*.jws</url-pattern>????
  15. ????</servlet-mapping>????
  16. ????<servlet-mapping>????
  17. ????????<servlet-name>axis</servlet-name>????
  18. ????????<url-pattern>/services/*</url-pattern>????
  19. ????</servlet-mapping>??



接着定义你的webservice的接口,继承ServletEndpointSupport,并在其onInit方法中加载Spring的bean:

Java代码 ?收藏代码
  1. public ? class ?Caculator?exends?ServletEndpointSupport{??
  2. ???protected ? void ?onInit()? throws ?ServiceException{??
  3. ????service?=?getApplicationContext().getBean("calculatorService?" );???
  4. ???}??
  5. ???public ?ResponseMsg?caculate( int ?a,? int ?b,?String?operation){??
  6. ????????return ?service.caculate(a,b,operation);??
  7. ???}??
  8. }??



下一步定义wsdd文件,声明该接口:

Java代码 ?收藏代码
  1. <?xml?version= "1.0" ?encoding= "UTF-8" ?>??
  2. <deployment?xmlns="http://xml.apache.org/axis/wsdd/" ??
  3. ????xmlns:java="http://xml.apache.org/axis/wsdd/providers/java" >??
  4. ????<globalConfiguration>??
  5. ????????<parameter?name="adminPassword" ?value= "admin" ?/>??
  6. ????????<parameter?name="sendXsiTypes" ?value= "true" ?/>??
  7. ????????<parameter?name="sendMultiRefs" ?value= "true" ?/>??
  8. ????????<parameter?name="sendXMLDeclaration" ?value= "true" ?/>??
  9. ????????<parameter?name="axis.sendMinimizedElements" ?value= "true" ?/>??
  10. ????????<requestFlow>??
  11. ????????????<handler?type="java:org.apache.axis.handlers.JWSHandler" >??
  12. ????????????????<parameter?name="scope" ?value= "session" ?/>??
  13. ????????????</handler>??
  14. ????????????<handler?type="java:org.apache.axis.handlers.JWSHandler" >??
  15. ????????????????<parameter?name="scope" ?value= "request" ?/>??
  16. ????????????????<parameter?name="extension" ?value= ".jwr" ?/>??
  17. ????????????</handler>??
  18. ????????</requestFlow>??
  19. ????</globalConfiguration>??
  20. ????<handler?name="Authenticate" ??
  21. ????????type="java:org.apache.axis.handlers.SimpleAuthenticationHandler" ?/>??
  22. ????<handler?name="LocalResponder" ??
  23. ????????type="java:org.apache.axis.transport.local.LocalResponder" ?/>??
  24. ????<handler?name="URLMapper" ?type= "java:org.apache.axis.handlers.http.URLMapper" ?/>??
  25. ??
  26. ????<!--?定义WebService的管理台?-->??
  27. ????<service?name="AdminService" ?provider= "java:MSG" >??
  28. ????????<parameter?name="allowedMethods" ?value= "AdminService" ?/>??
  29. ????????<parameter?name="enableRemoteAdmin" ?value= "false" ?/>??
  30. ????????<parameter?name="className" ?value= "org.apache.axis.utils.Admin" ?/>??
  31. ????????<namespace>http://xml.apache.org/axis/wsdd/</namespace> ??
  32. ????</service>??
  33. ??
  34. ???????<!--?定义你的服务接口?-->??
  35. ????<service?name="Caculator" ?provider= "java:RPC" ?use= "literal" >??
  36. ????????<parameter?name="allowedMethods" ?value= "*" ?/>??
  37. ????????<parameter?name="className" ??
  38. ????????????value="com.raymon.Caculator" ?/>??
  39. ????????<beanMapping?encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" ??
  40. ????????????serializer="org.apache.axis.encoding.ser.JDomElementSerializerFactory" ??
  41. ????????????deserializer="org.apache.axis.encoding.ser.JDomElementDeserializerFactory" ??
  42. ????????????languageSpecificType="java:com.raymon.bean.ResponseMsg" ??
  43. ????????????name="ns:ResponseMsg" ?regenerateElement= "true" ?xmlns:ns= "urn:BeanService" ?/>??
  44. ????</service>??
  45. ??
  46. ????<!--?定义WebSerivice的系统服务。?-->??
  47. ????<service?name="Version" ?provider= "java:RPC" >??
  48. ????????<parameter?name="allowedMethods" ?value= "getVersion" ?/>??
  49. ????????<parameter?name="className" ?value= "org.apache.axis.Version" ?/>??
  50. ????</service>??
  51. ????<transport?name="http" >??
  52. ????????<requestFlow>??
  53. ????????????<handler?type="URLMapper" ?/>??
  54. ????????????<handler?type="java:org.apache.axis.handlers.http.HTTPAuthHandler" ?/>??
  55. ????????</requestFlow>??
  56. ????</transport>??
  57. ????<transport?name="local" >??
  58. ????????<responseFlow>??
  59. ????????????<handler?type="LocalResponder" ?/>??
  60. ????????</responseFlow>??
  61. ????</transport>??
  62. </deployment>??



到此为止,webservice的服务器端已经定义结束,注意自定义返回类型ResponseMsg的配置。

客户端代码:

Java代码 ?收藏代码
  1. public ? static ? void ?main(String?[]?args){??
  2. ???try {??
  3. ?????String?endPoint?=?"http://1.1.1.1:8080/services/Caculator" ;??
  4. ?????Service?service?=?new ?Service();??
  5. ?????Call?call?=?(Call)service.createCall();??
  6. ?????Qname?qname?=?new ?Qname( "urn:BeanService" , "ResponseMsg" );??
  7. ?????call.registerTypeMapping(ResponseMsg.class ,qname, new ?BeanSerializerFactory(ResponseMsg. class ,qname), new ?BeanDeserializerFactory(ResponseMsg. class ,qname));??
  8. ??
  9. ????call.setReturnClass(ResponseMsg.class );??
  10. ????call.addParameter("in0" ,XMLType.XSD_INTEGER,ParameterMode.IN);??
  11. ????call.addParameter("in1" ,XMLType.XSD_STRING,ParameterMode.IN);??
  12. ????call.addParameter("in2" ,XMLType.XSD_STRING,ParameterMode.IN);??
  13. ??
  14. ????URL?url?=?new ?URL(endPoint);??
  15. ????call.setTargetEndpointAddress(url);??
  16. ????Qname?caculate?=?new ?Qname(endPoint, "caculate" );??
  17. ????call.setOperationName(caculate);??
  18. ????ResponseMsg?response?=?call.invoke(new ?Ojbect[]{ 2 , 3 , "*" });??
  19. ???}catch (Exception?e){??
  20. ???????e.printStackTrace();??
  21. ???}??
  22. }?
  相关解决方案