Axis1.4+Spring

 1.下载Axis,将Axis下的文件目录拷贝到项目的WEB-INFO下,需注意activation.jar、axis.jar和mail.jar这三个jar包也要复制到lib下;

2.在web.xml中添加Axis的servlet配置:

  
  
  
  
  1. <servlet> 
  2.    <servlet-name>AxisServlet</servlet-name> 
  3.    <display-name>Apache-Axis Servlet</display-name> 
  4.    <servlet-class> 
  5.        org.apache.axis.transport.http.AxisServlet 
  6.    </servlet-class> 
  7.    <load-on-startup>3</load-on-startup> 
  8.  </servlet> 
  9.  <servlet-mapping> 
  10.    <servlet-name>AxisServlet</servlet-name> 
  11.    <url-pattern>/servlet/AxisServlet</url-pattern> 
  12.  </servlet-mapping> 
  13.  <servlet-mapping> 
  14.    <servlet-name>AxisServlet</servlet-name> 
  15.    <url-pattern>*.jws</url-pattern> 
  16.  </servlet-mapping> 
  17.  <servlet-mapping> 
  18.    <servlet-name>AxisServlet</servlet-name> 
  19.    <url-pattern>/services/*</url-pattern> 
  20.  </servlet-mapping> 

在web.xml中修改struts2的配置,以免Axis被拦截:

  
  
  
  
  1. <filter> 
  2.         <filter-name>struts2</filter-name> 
  3.         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 
  4.     </filter> 
  5.      <filter-mapping> 
  6.       <filter-name>struts2</filter-name> 
  7.       <url-pattern>*.action</url-pattern> 
  8.      </filter-mapping> 
  9.      <filter-mapping> 
  10.       <filter-name>struts2</filter-name> 
  11.       <url-pattern>*.jsp</url-pattern> 
  12.      </filter-mapping> 
  13.      <filter-mapping> 
  14.       <filter-name>struts2</filter-name> 
  15.       <url-pattern>/struts/*</url-pattern> 
  16.      </filter-mapping> 

3.编写webservice类Axis:

 

  
  
  
  
  1. public class Axis extends ServletEndpointSupport { 
  2.  
  3.     private OrganSerivce organAxisService; 
  4.  
  5.     private UserService userAxisService; 
  6.      
  7.     public void sendMessage(MessageBO messageBO) throws Exception { 
  8. organAxisService = (OrganSerivce)getApplicationContext().getBean("organServiceImpl"); userAxisService = (UserService)getApplicationContext().getBean("userServiceImpl");
  9. System.out.printIn(messageBO.getMegCode());

注意:如果要整合spring的话,必须实现ServletEndpointSupport这个类,然后利用getApplicationContext().getBean来对Service进行注入,否则任何其他注入方法运行时Service都将为空。

4.编写deploy.wsdd,放在WEB-INFO下:

  
  
  
  
  1. <deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">    
  2.     <service name="Axis" provider="java:RPC">    
  3.         <parameter name="className" value="com.***.web.webservice.***.Axis"/>    
  4.         <parameter name="allowedMethods" value="*"/>  
  5.     </service>    
  6. </deployment>    

Axis为webservice名称,com.***.web.webservice.***.Axis为webservice类的全路径,其他无需修改;

5.编写deploy.bat,与deploy.wsdd相同都放在项目WEB-INFO下:

  
  
  
  
  1. set Axis_Lib=E:\eclipse-3.4\***\***\WebRoot\WEB-INF\lib 
  2. set Java_Cmd=java -Djava.ext.dirs=%Axis_Lib% 
  3. set Axis_Servlet=http://localhost:8080/项目名称/servlet/AxisServlet 
  4. %Java_Cmd% org.apache.axis.client.AdminClient -l%Axis_Servlet% deploy.wsdd 

6.启动项目,进入控制台,cd到E:\eclipse-3.4\***\***\WebRoot\WEB-INF下,执行deploy.bat,出现done之后,在项目的WEB-INFO下生成

server-config.wsdd:

  
  
  
  
  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="sendMultiRefs" value="true"/> 
  5.   <parameter name="disablePrettyXML" value="true"/> 
  6.   <parameter name="adminPassword" value="admin"/> 
  7.   <parameter name="attachments.Directory" value="E:\eclipse-3.4\huarui\zlyh\WebRoot\WEB-INF\attachments"/> 
  8.   <parameter name="dotNetSoapEncFix" value="true"/> 
  9.   <parameter name="enableNamespacePrefixOptimization" value="false"/> 
  10.   <parameter name="sendXMLDeclaration" value="true"/> 
  11.   <parameter name="sendXsiTypes" value="true"/> 
  12.   <parameter name="attachments.implementation" value="org.apache.axis.attachments.AttachmentsImpl"/> 
  13.   <requestFlow> 
  14.    <handler type="java:org.apache.axis.handlers.JWSHandler"> 
  15.     <parameter name="scope" value="session"/> 
  16.    </handler> 
  17.    <handler type="java:org.apache.axis.handlers.JWSHandler"> 
  18.     <parameter name="scope" value="request"/> 
  19.     <parameter name="extension" value=".jwr"/> 
  20.    </handler> 
  21.   </requestFlow> 
  22.  </globalConfiguration> 
  23.  <handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper"/> 
  24.  <handler name="LocalResponder" type="java:org.apache.axis.transport.local.LocalResponder"/> 
  25.  <handler name="Authenticate" type="java:org.apache.axis.handlers.SimpleAuthenticationHandler"/> 
  26.  
  27. <!--自己的webservice配置--> 
  28. <service name="Axis" provider="java:RPC"> 
  29.   <parameter name="allowedMethods" value="*"/> 
  30.   <parameter name="className" value="com.***.web.webservice.***.Axis"/> 
  31.   <beanMapping languageSpecificType="java:com.***.pojo.MessageBO" qname="ns1:MessageBO" xmlns:ns1="urn:BeanService"/> 
  32.  </service> 
  33.  
  34.  <service name="AdminService" provider="java:MSG"> 
  35.   <parameter name="allowedMethods" value="AdminService"/> 
  36.   <parameter name="enableRemoteAdmin" value="false"/> 
  37.   <parameter name="className" value="org.apache.axis.utils.Admin"/> 
  38.   <namespace>http://xml.apache.org/axis/wsdd/</namespace> 
  39.  </service> 
  40.  <service name="Version" provider="java:RPC"> 
  41.   <parameter name="allowedMethods" value="getVersion"/> 
  42.   <parameter name="className" value="org.apache.axis.Version"/> 
  43.  </service> 
  44.  <transport name="http"> 
  45.   <requestFlow> 
  46.    <handler type="URLMapper"/> 
  47.    <handler type="java:org.apache.axis.handlers.http.HTTPAuthHandler"/> 
  48.   </requestFlow> 
  49.   <parameter name="qs:list" value="org.apache.axis.transport.http.QSListHandler"/> 
  50.   <parameter name="qs:wsdl" value="org.apache.axis.transport.http.QSWSDLHandler"/> 
  51.   <parameter name="qs.list" value="org.apache.axis.transport.http.QSListHandler"/> 
  52.   <parameter name="qs.method" value="org.apache.axis.transport.http.QSMethodHandler"/> 
  53.   <parameter name="qs:method" value="org.apache.axis.transport.http.QSMethodHandler"/> 
  54.   <parameter name="qs.wsdl" value="org.apache.axis.transport.http.QSWSDLHandler"/> 
  55.  </transport> 
  56.  <transport name="local"> 
  57.   <responseFlow> 
  58.    <handler type="LocalResponder"/> 
  59.   </responseFlow> 
  60.  </transport> 
  61. </deployment> 

如需传递对象,就要另外在自己的webservice配置中添加:

  
  
  
  
  1. <beanMapping languageSpecificType="java:com.***.pojo.MessageBO" qname="ns1:MessageBO" xmlns:ns1="urn:BeanService"/>  
  2.  

告诉axis要将传递参数序列化;

7.测试:

  
  
  
  
  1. public class Test { 
  2.  
  3.     public static void main(String[] args) throws Exception { 
  4.         String targetEendPoint =  "http://localhost:8080/项目名称/services/Axis?wsdl" ;    
  5.         Service service =  new  Service();    
  6.         Call call = (Call) service.createCall();                
  7.         call.setOperationName( new  QName(targetEendPoint,  "sendMessage" ));    
  8.         QName qn = new QName("urn:BeanService","MessageBO");     
  9. call.registerTypeMapping(MessageBO.class, qn,   
  10.     new BeanSerializerFactory(MessageBO.class, qn), new BeanDeserializerFactory(MessageBO.class, qn)); 
  11.         call.setTargetEndpointAddress( new  URL(targetEendPoint));    
  12.         MessageBO messageBO = new MessageBO(); 
  13.         messageBO.setMegCode("409"); 
  14.         String result = (String) call.invoke( new  Object[]{messageBO});    
  15.         //System.out.println(result);    
  16.     } 
  17.  

注意:此句传递对象时才需要添加。

  1. call.registerTypeMapping(MessageBO.class, qn,   
  2.     new BeanSerializerFactory(MessageBO.class, qn), new BeanDeserializerFactory(MessageBO.class, qn));

你可能感兴趣的:(spring,webservice,axis)