本章节主要介绍Axis1.4发布WebService。这里只说明发布相关内容,调用方法后续会说明。
<dependency> <groupId>axis</groupId> <artifactId>axis</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>axis</groupId> <artifactId>axis-wsdl4j</artifactId> <version>1.5.1</version> </dependency> <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.7</version> </dependency>
public class HelloJWS { public String sayHello(String name){ return "jws = " + name; } }将上面java类后缀修改为jws复制到tomcat/webapps/axis/ 目录下。访问路径 :项目名/类名.jws?wsdl
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <servlet> <servlet-name>AxisServlet</servlet-name> <servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>AxisServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> </web-app>3. 新建deploy.wsdd文件,将我们服务发布出去。
<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"> <!-- name: 服务名随便写。 --> <service name="SayHello" provider="java:RPC"> <parameter name="className" value="com.ztesoft.axis.ws.HelloImpl" /> <!-- value表示哪些方法需要发布,*表示全部的public方法。 --> <!-- 如果想指定方法名,方法名与方法名用逗号隔开 --> <parameter name="allowedMethods" value="*" /> <!-- 指定命名空间 --> <namespace>http://www.ztesoft.com/axis/sayHello</namespace> </service> </deployment>
java -Djava.ext.dirs=lib org.apache.axis.client.AdminClient -l http://127.0.0.1:8080/axis/services deploy.wsdd注意:此处是相对路径,所以你需要到tomcat/webapps/axis/WEB-INF 目录下执行cmd。你就会看到
<deployment xmlns="http://xml.apache.org/axis/wsdd/" > <service name="SayHello" /> </deployment>总结:
<?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="dotNetSoapEncFix" value="true" /> <parameter name="enableNamespacePrefixOptimization" value="false" /> <parameter name="sendXMLDeclaration" value="true" /> <parameter name="attachments.implementation" value="org.apache.axis.attachments.AttachmentsImpl" /> <parameter name="sendXsiTypes" value="true" /> <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" /> <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> <!-- 想要添加或者取消修改一下内容即可,AdminService与Version注销不提供服务。 --> <service name="SayHello" provider="java:RPC"> <parameter name="allowedMethods" value="*" /> <parameter name="className" value="com.ztesoft.axis.ws.HelloImpl" /> <namespace>http://www.ztesoft.com/axis/sayHello</namespace> </service> </deployment>
package com.ztesoft.axis.ws; import org.apache.axis.AxisFault; import org.apache.axis.MessageContext; import org.apache.axis.handlers.BasicHandler; public class BeforeHandle extends BasicHandler { /** */ private static final long serialVersionUID = -1804045183252961242L; @Override public void invoke(MessageContext msgContext) throws AxisFault { String someParam = (String) this.getOption("someParam"); System.out.println("前置方法,这里会第一个被调用"); System.out.println("server-config.wsdd中配置的自定义参数 :" + someParam); } }
package com.ztesoft.axis.ws; import org.apache.axis.AxisFault; import org.apache.axis.MessageContext; import org.apache.axis.handlers.BasicHandler; public class AfterHandler extends BasicHandler{ /** */ private static final long serialVersionUID = -1077633556581583559L; @Override public void invoke(MessageContext msgContext) throws AxisFault { System.out.println("后置方法,执行完服务后调用我。我是第三个被调用的。"); } }2. 修改server-config.wsdd文件,type里面的类路径前面需要加上前缀 java:
<!-- 添加一个自定义的handler --> <handler name="beforeHandler" type="java:com.ztesoft.axis.ws.BeforeHandle"> <!-- 这里还可以设置自定义参数,在类中使用this.getOptions("someParam") 获取参数 --> <parameter name="someParam" value="好好学习,天天向上" /> </handler> <handler name="afterHandler" type="java:com.ztesoft.axis.ws.AfterHandler" /> <service name="SayHello" provider="java:RPC"> <parameter name="allowedMethods" value="*" /> <parameter name="className" value="com.ztesoft.axis.ws.HelloImpl" /> <namespace>http://www.ztesoft.com/axis/sayHello</namespace> <!-- 添加handler --> <requestFlow> <handler type="beforeHandler" /> </requestFlow> <responseFlow> <handler type="afterHandler" /> </responseFlow> </service>
package com.ztesoft.axis.ws; import org.apache.axis.SimpleChain; public class BeforeChain extends SimpleChain{ /** */ private static final long serialVersionUID = 7781745675745772330L; // 写在构造器重即可 public BeforeChain(){ BeforeHandle beforeHandle = new BeforeHandle(); AfterHandler afterHandler = new AfterHandler(); // 执行顺序与添加顺序一致。 this.addHandler(beforeHandle); this.addHandler(afterHandler); } }
package com.ztesoft.axis.ws; import org.apache.axis.SimpleChain; public class AfterChain extends SimpleChain { /** */ private static final long serialVersionUID = -620214048210047247L; public AfterChain(){ BeforeHandle beforeHandle = new BeforeHandle(); AfterHandler afterHandler = new AfterHandler(); // 执行顺序与添加顺序一致。 this.addHandler(afterHandler); this.addHandler(beforeHandle); } }
<!-- 添加自定义Chain --> <chain name="beforeChain"> <handler type="java:com.ztesoft.axis.ws.BeforeChain" /> </chain> <chain name="afterChain"> <handler type="java:com.ztesoft.axis.ws.AfterChain" /> </chain> <service name="SayHello" provider="java:RPC"> <parameter name="allowedMethods" value="*" /> <parameter name="className" value="com.ztesoft.axis.ws.HelloImpl" /> <!-- 添加handler --> <requestFlow> <chain type="beforeChain" /> </requestFlow> <responseFlow> <chain type="afterChain" /> </responseFlow> </service>总结:
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <servlet> <servlet-name>AxisServlet</servlet-name> <servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>AxisServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> </web-app>
<?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="dotNetSoapEncFix" value="true" /> <parameter name="enableNamespacePrefixOptimization" value="false" /> <parameter name="sendXMLDeclaration" value="true" /> <parameter name="attachments.implementation" value="org.apache.axis.attachments.AttachmentsImpl" /> <parameter name="sendXsiTypes" value="true" /> <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" /> <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> <!-- 添加一个自定义的handler --> <handler name="beforeHandler" type="java:com.ztesoft.axis.ws.BeforeHandle"> <!-- 这里还可以设置自定义参数,在类中使用this.getOptions("someParam") 获取参数 --> <parameter name="someParam" value="好好学习,天天向上" /> </handler> <handler name="afterHandler" type="java:com.ztesoft.axis.ws.AfterHandler" /> <!-- 添加自定义Chain --> <chain name="beforeChain"> <handler type="java:com.ztesoft.axis.ws.BeforeChain" /> </chain> <chain name="afterChain"> <handler type="java:com.ztesoft.axis.ws.AfterChain" /> </chain> <service name="SayHello" provider="java:RPC"> <parameter name="allowedMethods" value="*" /> <parameter name="className" value="com.ztesoft.axis.ws.HelloImpl" /> <namespace>http://www.ztesoft.com/axis/sayHello</namespace> <!-- 添加handler或者Chain --> <requestFlow> <!-- <handler type="beforeHandler"/> --> <chain type="beforeChain" /> </requestFlow> <responseFlow> <!-- <handler type="afterHandler"/> --> <chain type="afterChain" /> </responseFlow> </service> </deployment>