根
据网上搜索到的一些例子做的时候碰到了挺多的问题,经过不懈的努力终于完成了这个webservice的例子,实际上axis的文档上也写了一个例子,网
上的例子也大多都是照他上面所写的那样,只是有些讲的不算很详细,所以产生了不少错误,小弟知识浅薄,如有写的不清楚地地方,还请见谅
所需软件
tomcat :http://tomcat.apache.org/index.html
axis :http://ws.apache.org/axis/
jdk :http://java.sun.com/javase/downloads/index.jsp
jaf :http://java.sun.com/products/javabeans/jaf/downloads/index.html
xmlrpc :http://ws.apache.org/xmlrpc
XML解析器
Xalan :http://archive.apache.org/dist/xml/xalan-j/
Xerces :http://xml.apache.org/dist/xerces-j/
本例讲解axis定制发布
本实例采用
j2sdk1.4.2_05,tomcat5.0.28,axis1.4,jbuilder X / Eclipse3.2
首先将axis解压至相应目录,如d:\下,安装tomcat至c:\tomcat5,安装jdk至c:\ j2sdk1.4.2_05
然后将d:\axis\webapp目录下的axis目录copy至c:\tomcat5\webapps目录下
设置axis的环境变量
AXIS_HOME=d:\axis
AXIS_LIB=%AXIS_HOME%\lib
AXISCLASSPATH=%AXIS_LIB%\axis.jar;%AXIS_LIB%\commons-discovery-0.2.jar;%AXIS_LIB%\commons-logging-1.0.4.jar;%AXIS_LIB%\jaxrpc.jar;%AXIS_LIB%\saaj.jar;%AXIS_LIB%\log4j-1.2.8.jar;%AXIS_LIB%\xml-apis.jar;%AXIS_LIB%\xercesImpl.jar;%AXIS_LIB%\wsdl4j-1.5.1.jar;%AXIS_LIB%\activation.jar;%AXIS_LIB%\xmlrpc-2.0.jar
(xml-apis.jar, xercesImpl.jar属于Xerces或Xalan, activation.jar属于jaf, xmlrpc-2.0.jar属于xmlrpc)
1. 配置完成后启动tomcat,在浏览器中键入http://localhost:8080/axis,将显示如下画面
2.在C:\Tomcat5\webapps\axis\WEB-INF目录下建立一个src目录,用于存放源程序
接着编写服务端程序server.AxisReturn
package server;
public class AxisReturn {
public String ReturnMsg(String servicesName) {
return "Axis Return: "+ servicesName;
}
}
★在
此我们利用了Jbuilder X作为我们java程序的开发工具,利用Jbuilder新建工程中的Project for Existing
Code将C:\Tomcat5\webapps\axis\WEB-INF下的程序及目录导入作为Jbuilder的一个工程来操作
编译AxisReturn程序,Jbuilder将会把编译后的class文件自存放在C:\Tomcat5\webapps\axis\WEB-INF\classes\server目录下
3.编写wsdd文件,此处命名为deploy.wsdd,其内容为
<deployment xmlns=http://xml.apache.org/axis/wsdd/
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<service name="AxisReturn" provider="java:RPC">
<parameter name="className" value="server.AxisReturn"/>
<parameter name="allowedMethods" value="*"/>
</service>
</deployment>
★每个service就代表服务端的一个程序,如有多个可继续添加service,相关参数请查阅axis文档
★此处的deployment代表发布服务,如改为undeployment则为撤销服务
4.发布服务
打开windows的命令窗口,转到wsdd文件的存放目录下,此处为C:\Tomcat5\webapps\axis\WEB-INF\src\server
在命令窗口中键入
java -cp %AXISCLASSPATH% org.apache.axis.client.AdminClient deploy.wsdd
如果成功将显示<Admin>Done processing</Admin>
★如失败,则检查AXISCLASSPATH是否设置正确,tomcat端口是否为默认的8080
★在这里有一个问题需注意,不管是在axis目录下还是其他虚拟目录下,如直接执行这条命令,都将在axis的目录下发布service,如果想发布在其他的目录下,如pscsaxis,则应执行
java -cp %AXISCLASSPATH% org.apache.axis.client.AdminClient -lhttp://localhost:8080/pscsaxis/services/AdminService deploy.wsdd
-cp表示在AXISCLASSPATH环境变量中检索org.apache.axis.client.AdminClient方法
5.生成client stub文件
★在命令窗口中将目录转至C:\Tomcat5\webapps\axis\WEB-INF\classes
然后执行
java
-cp %AXISCLASSPATH% org.apache.axis.wsdl.Java2WSDL -oAxisReturn.wsdl
-lhttp://localhost:8080/axis/services/AxisReturn -nAxisReturn
server.AxisReturn
将会在C:\Tomcat5\webapps\pscsaxis\WEB-INF\classes下生成AxisReturn.wsdl文件
接着在执行
java -cp %AXISCLASSPATH% org.apache.axis.wsdl.WSDL2Java AxisReturn.wsdl -p client
将会在C:\Tomcat5\webapps\axis\WEB-INF\classes\client目录下生成四个java文件
AxisReturn_PortType.java
AxisReturnService.java
AxisReturnServiceLocator.java
AxisReturnSoapBindingStub.java
将这四个文件剪切至C:\Tomcat5\webapps\pscsaxis\WEB-INF\src\client目录下,并编译
6.编写客户端
利用生成client stub文件编写
package client;
public class ClientAxisReturn {
public static void main(String args[]) {
try {
AxisReturnService service = new client.AxisReturnServiceLocator();
client.AxisReturn_PortType client = service.getAxisReturn();
String retValue = client.returnMsg("BaoSteel");
System.out.println(retValue);
}
catch (Exception e) {
System.err.println("Execution failed. Exception: " + e);
}
}
}
编写完后,编译运行即可得到返回结果
★如需传入多个参数,需在String retValue = client.returnMsg("BaoSteel");这句中增加参数,如
String retValue = client.returnMsg("BaoSteel","PSCS_IMS");
★相应的服务端也要更改,如
package server;
public class AxisReturn {
public String ReturnMsg(String servicesName,String serviceID) {
return "Axis Return: "+ servicesName+ serviceID;
}
}
利用发布服务的wsdl的URL编写
package client;
import org.apache.axis.AxisFault;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.encoding.XMLType;
public class ClientAxisReturnWsdl {
public static void main(String args[]) throws Exception{
String endPoint =
http://190.2.63.239:8080/pscsaxis/services/AxisReturn?wsdl;
Service service = new Service();
Call call = (Call) service.createCall();
Object result;
try {
call.setTargetEndpointAddress(new java.net.URL(endPoint));
call.setOperationName("ReturnMsg");
call.addParameter("OP1", XMLType.XSD_STRING, ParameterMode.IN);
call.setReturnType(XMLType.XSD_STRING);
result = (Object) call.invoke(new Object[] {"BaoSteel"});
}
catch (AxisFault fault) {
result = "Error is: " + fault.toString();
}
System.out.println(result);
}
}
编写完后,编译运行即可得到返回结果
★如许增加参数,则需添加语句
call.addParameter("OP1", XMLType.XSD_STRING, ParameterMode.IN);
将参数名OP1改为其他,如OP2
在result = (Object) call.invoke(new Object[] {" BaoSteel "});这句中增加参数,如
result = (Object) call.invoke(new Object[] {" BaoSteel "," PSCS_IMS "});
★相应的服务端也要更改,如
package server;
public class AxisReturn {
public String ReturnMsg(String servicesName,String serviceID) {
return "Axis Return: "+ servicesName+ serviceID;
}
}
★在这里,我们的服务端只有一个方法供我们调用,如果我们需要调用多个服务端的方法,则可在服务端中加入其他方法,如我们在增加一个xxMsg
package server;
public class AxisReturn {
public String ReturnMsg(String servicesName,String systemID) {
return "Axis Return: "+ str+servicesName+systemID;
}
public String xxMsg(String serviceName,String systemID) {
return "XX Return: "+ str+serviceName+systemID;
}
}
★修改完后将其编译,并将其注销后重新发布,反之需要重起tomcat,否则服务将不被更新
★利用生成client stub文件编写的客户端程序,还需重新生成client stub文件,并将其编译,在客户端程序中调用其相应得方法即可
SOAPMonitor的配置
发
布了Web服务以后,如何观察请求和响应数据呢?记录运行日志是一种传统且有效的方法,但对于调试程序来讲还不够方便和直观。值得欣慰的是,Axis为我
们提供了在客户端观察SOAP请求和响应流数据的工具SoapMonitor,经过适当配置后,可以实时地观察到Web服务的SOAP请求和响应数据。
SoapMonitor是一个Java Applet程序,通过浏览器下载到客户端运行。下面就介绍SoapMonitor的配置和使用方法。
在C:\Tomcat5\webapps\axis的目录下有一个SOAPMonitorApplet.java的程序,axis默认没有给我们编译,我们需要自己进行编译
打开windows命令窗口,转到C:\Tomcat5\webapps\axis目录下,执行
javac -classpath %AXIS_HOME%\lib\axis.jar SOAPMonitorApplet.java
编译完成后需要发布服务,我们需要建立一个wsdd文件deploy-monitor.wsdd,内容如下
<deployment xmlns=http://xml.apache.org/axis/wsdd/
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<handler name="soapmonitor"
type="java:org.apache.axis.handlers.SOAPMonitorHandler">
<parameter name="wsdlURL" value="/axis/SOAPMonitorService-impl.wsdl"/>
<parameter name="namespace" value="http://tempuri.org/wsdl/2001/12/SOAPMonitorService-impl.wsdl"/>
<parameter name="serviceName" value="SOAPMonitorService"/>
<parameter name="portName" value="Demo"/>
</handler>
<service name="SOAPMonitorService" provider="java:RPC">
<parameter name="allowedMethods" value="publishMessage"/>
<parameter name="className" value="org.apache.axis.monitor.SOAPMonitorService"/>
<parameter name="scope" value="Application"/>
</service>
</deployment>
★需要注意的是红色的那句语句,如果是在我们自己的web应用目录下的话需改成自己的目录。
建立完成后执行命令进行发布
java
-cp %AXISCLASSPATH% org.apache.axis.client.AdminClient
-lhttp://localhost:8080/axis/services/AdminService deploy-monitor.wsdd
发布SOAPMonitor服务后,还要对被监测的Web服务进行配置。方法是先注销该Web服务,然后修改该服务对应的WSDD文件,在其中增加请求流和响应流的配置,否则是观测不到SOAP请求和响应流的。以我们上面的程序为例,将deploy.wsdd修改为
<deployment xmlns=http://xml.apache.org/axis/wsdd/
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<service name="AxisReturn" provider="java:RPC">
<requestFlow>
<handler type="soapmonitor"/>
</requestFlow>
<responseFlow>
<handler type="soapmonitor"/>
</responseFlow>
<parameter name="className" value="server.AxisReturn"/>
<parameter name="allowedMethods" value="*"/>
</service>
</deployment>
增加了
<requestFlow>
<handler type="soapmonitor"/>
</requestFlow>
<responseFlow>
<handler type="soapmonitor"/>
</responseFlow>
这两段
然后我们通过点击http://localhost:8080/axis/主页上的SOAPMonitor或直接访问http://localhost:8080/axis/SOAPMonitor,点击start或stop启动或停止监控,然后我们运行客户端程序,SOAPMonitor会监控到请求和响应流,如下图
★在
这里,我们需要注意一个配置问题,如果tomcat下有多个axis应用,设置了多个SOAPmonitor,我们则需要更改SOAPmonitor的端
口(axis默认配置的是5001),否则会产生冲突,运行SOAPmonitor时会无法启动,提示the soap monitor is
unable to communcate with the server,解决方法如下:
打开C:\Tomcat5\webapps\axis\WEB-INF目录下的web.xml,找到
<servlet>
<servlet-name>SOAPMonitorService</servlet-name>
<display-name>SOAPMonitorService</display-name>
<servlet-class>
org.apache.axis.monitor.SOAPMonitorService
</servlet-class>
<init-param>
<param-name>SOAPMonitorPort</param-name>
<param-value>5001</param-value>
</init-param>
<load-on-startup>100</load-on-startup>
</servlet>
这段,将其中的5001改成5002,依此类推
总结:
尽量将发布websrrvice的文件夹和axis分开放置
建立多个axis应用时,应注意一些端口的设置,命令的参数设置以及一些命令执行的路径设置等,否则会产生一些错误,如ClassNotFound,service发布错误等的错误
server-config.wsdd
<?xml version="1.0" encoding="UTF-8"?>
<deployment xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"
xmlns:handler="http://xml.apache.org/axis/wsdd/providers/handler"
xmlns="http://xml.apache.org/axis/wsdd/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
name="defaultClientConfig" xsi:type="deployment">
<globalConfiguration>
<parameter name="disablePrettyXML" value="true"/>
<parameter name="dotNetSoapEncFix" value="true"/>
<parameter name="enableNamespacePrefixOptimization" value="false"/>
<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"/>
<service name="CallcenterWebServices" provider="java:RPC" style="rpc" use="encoded">
<parameter name="scope" value="Request"/>
<parameter name="className" value="com.isw2.ebay.callcenter.axis.CallcenterWebServices"/>
<parameter name="allowedMethods"
value="getStatus,getCsrBean,setStatus,createCase,showContactRecord,getWorkload"/>
<beanMapping qname="myNS:CsrBean" xmlns:myNS="urn:CsrBean"
languageSpecificType="java:com.isw2.ebay.callcenter.axis.CsrBean" />
<beanMapping qname="myNS:ContactRecordBean" xmlns:myNS="urn:ContactRecordBean"
languageSpecificType="java:com.isw2.ebay.callcenter.axis.ContactRecordBean" />
<beanMapping qname="myNS:DisputeContactRecord" xmlns:myNS="urn:DisputeContactRecord"
languageSpecificType="java:com.isw2.ebay.callcenter.axis.DisputeContactRecord" />
<beanMapping qname="myNS:WorkloadNumberBean" xmlns:myNS="urn:WorkloadNumberBean"
languageSpecificType="java:com.isw2.ebay.callcenter.axis.WorkloadNumberBean" />
</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">
<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:wsdl" value="org.apache.axis.transport.http.QSWSDLHandler"/>
<requestFlow>
<handler type="URLMapper"/>
<handler type="java:org.apache.axis.handlers.http.HTTPAuthHandler"/>
</requestFlow>
</transport>
<transport name="local">
<responseFlow>
<handler type="LocalResponder"/>
</responseFlow>
</transport>
</deployment>