1.下载Axis,将Axis下的文件目录拷贝到项目的WEB-INFO下,需注意activation.jar、axis.jar和mail.jar这三个jar包也要复制到lib下;
2.在web.xml中添加Axis的servlet配置:
- <servlet>
- <servlet-name>AxisServlet</servlet-name>
- <display-name>Apache-Axis Servlet</display-name>
- <servlet-class>
- org.apache.axis.transport.http.AxisServlet
- </servlet-class>
- <load-on-startup>3</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>AxisServlet</servlet-name>
- <url-pattern>/servlet/AxisServlet</url-pattern>
- </servlet-mapping>
- <servlet-mapping>
- <servlet-name>AxisServlet</servlet-name>
- <url-pattern>*.jws</url-pattern>
- </servlet-mapping>
- <servlet-mapping>
- <servlet-name>AxisServlet</servlet-name>
- <url-pattern>/services/*</url-pattern>
- </servlet-mapping>
在web.xml中修改struts2的配置,以免Axis被拦截:
- <filter>
- <filter-name>struts2</filter-name>
- <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>*.action</url-pattern>
- </filter-mapping>
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>*.jsp</url-pattern>
- </filter-mapping>
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>/struts/*</url-pattern>
- </filter-mapping>
3.编写webservice类Axis:
- public class Axis extends ServletEndpointSupport {
- private OrganSerivce organAxisService;
- private UserService userAxisService;
- public void sendMessage(MessageBO messageBO) throws Exception {
- organAxisService = (OrganSerivce)getApplicationContext().getBean("organServiceImpl"); userAxisService = (UserService)getApplicationContext().getBean("userServiceImpl");
- System.out.printIn(messageBO.getMegCode());
- }
- {
注意:如果要整合spring的话,必须实现ServletEndpointSupport这个类,然后利用getApplicationContext().getBean来对Service进行注入,否则任何其他注入方法运行时Service都将为空。
4.编写deploy.wsdd,放在WEB-INFO下:
- <deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
- <service name="Axis" provider="java:RPC">
- <parameter name="className" value="com.***.web.webservice.***.Axis"/>
- <parameter name="allowedMethods" value="*"/>
- </service>
- </deployment>
Axis为webservice名称,com.***.web.webservice.***.Axis为webservice类的全路径,其他无需修改;
5.编写deploy.bat,与deploy.wsdd相同都放在项目WEB-INFO下:
- set Axis_Lib=E:\eclipse-3.4\***\***\WebRoot\WEB-INF\lib
- set Java_Cmd=java -Djava.ext.dirs=%Axis_Lib%
- set Axis_Servlet=http://localhost:8080/项目名称/servlet/AxisServlet
- %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:
- <?xml version="1.0" encoding="UTF-8"?>
- <deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
- <globalConfiguration>
- <parameter name="sendMultiRefs" value="true"/>
- <parameter name="disablePrettyXML" value="true"/>
- <parameter name="adminPassword" value="admin"/>
- <parameter name="p_w_uploads.Directory" value="E:\eclipse-3.4\huarui\zlyh\WebRoot\WEB-INF\p_w_uploads"/>
- <parameter name="dotNetSoapEncFix" value="true"/>
- <parameter name="enableNamespacePrefixOptimization" value="false"/>
- <parameter name="sendXMLDeclaration" value="true"/>
- <parameter name="sendXsiTypes" value="true"/>
- <parameter name="p_w_uploads.implementation" value="org.apache.axis.p_w_uploads.AttachmentsImpl"/>
- <requestFlow>
- <handler type="java:org.apache.axis.handlers.JWSHandler">
- <parameter name="scope" value="session"/>
- </handler>
- <handler type="java:org.apache.axis.handlers.JWSHandler">
- <parameter name="scope" value="request"/>
- <parameter name="extension" value=".jwr"/>
- </handler>
- </requestFlow>
- </globalConfiguration>
- <handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper"/>
- <handler name="LocalResponder" type="java:org.apache.axis.transport.local.LocalResponder"/>
- <handler name="Authenticate" type="java:org.apache.axis.handlers.SimpleAuthenticationHandler"/>
- <!--自己的webservice配置-->
- <service name="Axis" provider="java:RPC">
- <parameter name="allowedMethods" value="*"/>
- <parameter name="className" value="com.***.web.webservice.***.Axis"/>
- <beanMapping languageSpecificType="java:com.***.pojo.MessageBO" qname="ns1:MessageBO" xmlns:ns1="urn:BeanService"/>
- </service>
- <service name="AdminService" provider="java:MSG">
- <parameter name="allowedMethods" value="AdminService"/>
- <parameter name="enableRemoteAdmin" value="false"/>
- <parameter name="className" value="org.apache.axis.utils.Admin"/>
- <namespace>http://xml.apache.org/axis/wsdd/</namespace>
- </service>
- <service name="Version" provider="java:RPC">
- <parameter name="allowedMethods" value="getVersion"/>
- <parameter name="className" value="org.apache.axis.Version"/>
- </service>
- <transport name="http">
- <requestFlow>
- <handler type="URLMapper"/>
- <handler type="java:org.apache.axis.handlers.http.HTTPAuthHandler"/>
- </requestFlow>
- <parameter name="qs:list" value="org.apache.axis.transport.http.QSListHandler"/>
- <parameter name="qs:wsdl" value="org.apache.axis.transport.http.QSWSDLHandler"/>
- <parameter name="qs.list" value="org.apache.axis.transport.http.QSListHandler"/>
- <parameter name="qs.method" value="org.apache.axis.transport.http.QSMethodHandler"/>
- <parameter name="qs:method" value="org.apache.axis.transport.http.QSMethodHandler"/>
- <parameter name="qs.wsdl" value="org.apache.axis.transport.http.QSWSDLHandler"/>
- </transport>
- <transport name="local">
- <responseFlow>
- <handler type="LocalResponder"/>
- </responseFlow>
- </transport>
- </deployment>
如需传递对象,就要另外在自己的webservice配置中添加:
- <beanMapping languageSpecificType="java:com.***.pojo.MessageBO" qname="ns1:MessageBO" xmlns:ns1="urn:BeanService"/>
告诉axis要将传递参数序列化;
7.测试:
- public class Test {
- public static void main(String[] args) throws Exception {
- String targetEendPoint = "http://localhost:8080/项目名称/services/Axis?wsdl" ;
- Service service = new Service();
- Call call = (Call) service.createCall();
- call.setOperationName( new QName(targetEendPoint, "sendMessage" ));
- QName qn = new QName("urn:BeanService","MessageBO");
- call.registerTypeMapping(MessageBO.class, qn,
- new BeanSerializerFactory(MessageBO.class, qn), new BeanDeserializerFactory(MessageBO.class, qn));
- call.setTargetEndpointAddress( new URL(targetEendPoint));
- MessageBO messageBO = new MessageBO();
- messageBO.setMegCode("409");
- String result = (String) call.invoke( new Object[]{messageBO});
- //System.out.println(result);
- }
- }
注意:此句传递对象时才需要添加。
- call.registerTypeMapping(MessageBO.class, qn,
- new BeanSerializerFactory(MessageBO.class, qn), new BeanDeserializerFactory(MessageBO.class, qn));