ITeye博客

用axis开发webservice实践

ling091 · 2007-03-19 14:35 · Web Service · Java · Apache · WebService · Weblogic · SOAP · Markdown

原文http://www.cnblogs.com/showna/articles/523038.html

什么是SOAP?

  SOAP是一个基于XML的用于应用程序之间通信数据编码的传输协议。最初由微软和Userland Software提出,随着不断地完善和改进,SOAP很快被业界广泛应用。

  什么是Axis?

  Axis本质上就是一个SOAP引擎,提供创建服务器端、客户端和网关SOAP操作的基本框架。Axis目前版本是为Java编写的,不过为C++的版本正在开发中。

  配置webservice具体步骤:

  1.安装weblogic。

  2.配置一个web应用,如“\\lijunjie\e$\software\bea\user_projects\domains\lijjdomain\applications\ cntapws “。

  3.从apache网站上下载axis-bin-1_2_1.tar.gz,解压后copy到cntapws下面。

  4.打开“http://localhost:7001/console“,配置cntapws 的web应用。

  5.axis1.2和weblogic8.1有冲突,原因是weblogic 本身携带的webservice.jar使用的旧的javax.xml.soap.*,而不是像axis使用J2EE1.4新的版本。解决方案是在WEB-INF/weblogic.xml文件中如下的配置:

    <weblogic-web-app>
 <container-descriptor>
  <prefer-web-inf-classes>true</prefer-web-inf-classes>
</container-descriptor>
</weblogic-web-app>
 

  6.按 http://localhost:8080/axis/happyaxis.jsp地址,可以显示你发布的webservice是否配置成功,如果显示必须的包缺失,则你的webservice不能正常运行。

  开发webservice流程

  1.写普通的java类,该类中有你要发布到webservice上的有关方法。

  2.配置server-comfig.xml, 把你要发布的方法写入该文件。具体内容如下:

xml 代码

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
  3. <globalConfiguration>
  4. <parameter name="adminPassword" value="admin"/>
  5. <parameter name="attachments.Directory" value="C:\eclipse\workspace\bookstore\WEB-INF\attachments"/>
  6. <parameter name="attachments.implementation" value="org.apache.axis.attachments.AttachmentsImpl"/>
  7. <parameter name="sendXsiTypes" value="true"/>
  8. <parameter name="sendMultiRefs" value="true"/>
  9. <parameter name="sendXMLDeclaration" value="true"/>
  10. <parameter name="axis.sendMinimizedElements" value="true"/>
  11. <requestFlow>
  12. <handler type="java:org.apache.axis.handlers.JWSHandler">
  13. <parameter name="scope" value="session"/>
  14. </handler>
  15. <handler type="java:org.apache.axis.handlers.JWSHandler">
  16. <parameter name="scope" value="request"/>
  17. <parameter name="extension" value=".jwr"/>
  18. </handler>
  19. </requestFlow>
  20. </globalConfiguration>
  21. <handler name="LocalResponder" type="java:org.apache.axis.transport.local.LocalResponder"/>
  22. <handler name="Authenticate" type="java:org.apache.axis.handlers.SimpleAuthenticationHandler"/>
  23. <handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper"/>
  24. <service name="Version" provider="java:RPC">
  25. <parameter name="allowedMethods" value="getVersion"/>
  26. <parameter name="className" value="org.apache.axis.Version"/>
  27. </service>
  28. <!--要配置的内容start,注意每一个要发布成webservice的类都要配置成一个service如下à
  29. <service name="webserviceproxy" provider="java:RPC">
  30. <parameter name="className" value="com.copia.webservice.server.proxy.WebServiceProxy"/>
  31. <parameter name="allowedMethods" value="*"/>
  32. <!—具体某个方法说明-->
  33. <operation name="login" returnType="ns:LoginOutServiceBean">
  34. <parameter name="branchNo" type="xsd:string"/>
  35. <parameter name="userId" type="xsd:string"/>
  36. <parameter name="password" type="xsd:string"/>
  37. <parameter name="certificateNo" type="xsd:string"/>
  38. </operation>
  39. <!—如果该方法的参数有基本类型和对象类型,则不要声明,webservice能够找到相关的类及方法-->
  40. <!—声明涉及到的所有类及其父类,引用类,还有它们的相关类,类似与对象树遍历,某个类声明缺失也会导致运行失败-->
  41. <typeMapping deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory"
  42. encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
  43. qname="ns:LoginOutServiceBean"
  44. serializer="org.apache.axis.encoding.ser.BeanSerializerFactory"
  45. type="java:com.copia.core.module.security.login.LoginOutServiceBean" xmlns:ns="webserviceproxy"/>
  46. <typeMapping deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory"
  47. encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
  48. qname="ns:User"
  49. serializer="org.apache.axis.encoding.ser.BeanSerializerFactory"
  50. type="java:com.copia.core.client.User" xmlns:ns="webserviceproxy"/>
  51. <typeMapping deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory"
  52. encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
  53. qname="ns:GetBatchListInServiceBean"
  54. serializer="org.apache.axis.encoding.ser.BeanSerializerFactory"
  55. type="java:com.copia.core.module.batch.service.GetBatchListInServiceBean" xmlns:ns="webserviceproxy"/>
  56. <typeMapping deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory"
  57. encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
  58. qname="ns:GetBatchListOutServiceBean"
  59. serializer="org.apache.axis.encoding.ser.BeanSerializerFactory"
  60. type="java:com.copia.core.module.batch.service.GetBatchListOutServiceBean" xmlns:ns="webserviceproxy"/>
  61. <typeMapping deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory"
  62. encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
  63. qname="ns:BatchJobDefBean"
  64. serializer="org.apache.axis.encoding.ser.BeanSerializerFactory"
  65. type="java:com.copia.core.module.batch.service.BatchJobDefBean" xmlns:ns="webserviceproxy"/>
  66. <typeMapping deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory"
  67. encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
  68. qname="ns:ValueObjectPage"
  69. serializer="org.apache.axis.encoding.ser.BeanSerializerFactory"
  70. type="java:com.copia.core.voa.ValueObjectPage" xmlns:ns="webserviceproxy"/>
  71. <typeMapping deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory"
  72. encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
  73. qname="ns:ValueObjectColInfo"
  74. serializer="org.apache.axis.encoding.ser.BeanSerializerFactory"
  75. type="java:com.copia.core.voa.ValueObjectColInfo" xmlns:ns="webserviceproxy"/>
  76. <typeMapping deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory"
  77. encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
  78. qname="ns:ValueObjectList"
  79. serializer="org.apache.axis.encoding.ser.BeanSerializerFactory"
  80. type="java:com.copia.core.voa.ValueObjectList" xmlns:ns="webserviceproxy"/>
  81. <typeMapping deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory"
  82. encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
  83. qname="ns:PageInfo"
  84. serializer="org.apache.axis.encoding.ser.BeanSerializerFactory"
  85. type="java:com.copia.core.voa.PageInfo" xmlns:ns="webserviceproxy"/>
  86. <typeMapping deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory"
  87. encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
  88. qname="ns:ValueObjectMetaData"
  89. serializer="org.apache.axis.encoding.ser.BeanSerializerFactory"
  90. type="java:com.copia.core.voa.ValueObjectMetaData" xmlns:ns="webserviceproxy"/>
  91. </service>
  92. <!--要配置的内容endà
  93. <service name="AdminService" provider="java:MSG">
  94. <parameter name="allowedMethods" value="AdminService"/>
  95. <parameter name="enableRemoteAdmin" value="false"/>
  96. <parameter name="className" value="org.apache.axis.utils.Admin"/>
  97. <namespace>http://xml.apache.org/axis/wsdd/</namespace>
  98. </service>
  99. <transport name="local">
  100. <responseFlow>
  101. <handler type="LocalResponder"/>
  102. </responseFlow>
  103. </transport>
  104. <transport name="http">
  105. <requestFlow>
  106. <handler type="URLMapper"/>
  107. <handler type="java:org.apache.axis.handlers.http.HTTPAuthHandler"/>
  108. </requestFlow>
  109. </transport>
  110. </deployment>

  3.在地址栏输入http://localhost:7001/cntapws/services/webserviceproxy?wsdl,如没有什么错误,则表示你的webservice发布成功了。

  Java  webservice客户端编程

  1.启动你配置好的weblobic。

  2.进入dos界面,在你的临时目录(任意目录)下根据wsdl产生客户文件,如下指令:

E:\kk>java -Djava.ext.dirs=E:\software\bea\user_projects\domains\lijjdomain\appl

ications\axis\WEB-INF\axislib org.apache.axis.wsdl.WSDL2Java http://localhost:7001/cntapws/services/webserviceproxy?wsdl即可在kk目录下产生有关文件。

  3.把产生的文件copy到你的工程中,再写一个webservice调用测试类,如下:

java 代码

  1. public class TestWs {
  2. public static void main(String[] args) {
  3. WebServiceProxyService service = new WebServiceProxyServiceLocator();
  4. try {
  5. WebServiceProxy serviceProxy = service.getwebserviceproxy();
  6. User user = new User();
  7. user.setBranchNo("000001");
  8. user.setClerkNo("1004");
  9. GetBatchListInServiceBean in = new GetBatchListInServiceBean();
  10. in.setCurPageNo(1);
  11. String serviceName = "GetBatchListService";
  12. GetBatchListOutServiceBean out = (GetBatchListOutServiceBean)serviceProxy.getBatchListService(user, in, serviceName);
  13. BatchJobDefBean[] defBeans = out.getBatchJobDefBeans();
  14. for(int i = 0 ; i < defBeans.length ; i++){
  15. System.out.println(defBeans[i].getBatchName());
  16. }
  17. }catch(Exception ex){
  18. ex.printStackTrace();
  19. }
  20. }
  21. }

这样,我们就完成了webservice的全部工作。大功告成了

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。