前段时间,系统需要对外发布web service接口,采用的是Axis,主要是参考网上的一篇文章,mornitor和自定义类型是自己加上的:
一、Axis环境的安装
1、安装环境 J2SE SDK,Tomcat,eclipse。
2、到 http://xml.apache.org 网站下载Axis安装包。
3、将Axis相关包文件放在WEB-INF\lib目录下,需要另外下载activation.jar。
Axis支持三种web service的部署和开发,分别为:
1、Dynamic Invocation Interface (DII)
2、Dynamic Proxy方式
3、Stubs方式
前两种方式我就不介绍了,同事告诉我他们自己都不使用前两种方式,他们建议我们使用Stubs方式,因此我就主要就介绍一下第三种方式。注意,我自己的Java源代码是放在D:\workspace\test\目录下,Axis相关包文件放在D:\workspace\test\WEB-INF目录下。
二、编写wsdd发布web服务,编写stub client访问web服务
1、编写服务端程序server,SayHello.java,编译server.SayHello.java
package server;
public class SayHello
{
public String getName(String name)
{
return "hello "+name;
}
}
2、编写wsdd文件
deploy.wsdd文件内容如下:
<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<service name="SayHello" provider="java:RPC">
<parameter name="className" value="server.SayHello.getName"/>
<parameter name="allowedMethods" value="*"/>
</service>
</deployment>
3、发布服务:
编辑一个deploy.bat,Axis_Lib为axis.jar路径。内容如下(注意目录名中不要包含空格):
set Axis_Lib=D:\workspace\test\WEB-INF\lib
set Java_Cmd=java -Djava.ext.dirs=%Axis_Lib%
set Axis_Servlet=http://localhost:8080/test/servlet/AxisServlet
%Java_Cmd% org.apache.axis.client.AdminClient -l%Axis_Servlet% deploy.wsdd
执行这个批处理文件,执行这个批处理之前,tomcat必须启动起来。如果处理成功的话,然后将class文件拷贝到axis的axis"WEB-INF"classes目录中,然后访问http://localhost:8080/test/services 就会显示服务列表。
4、生成客户端client stub文件
在浏览器上访问服务器端的服务,可以下载到WSDL文件,通过Axis的相关工具,可以自动从WSDL文件中生成Web Service的客户端代码。
编写一个WSDL2Java.bat文件,其内容如下:
set Axis_Lib=D:\workspace\test\WEB-INF\lib
set Java_Cmd=java -Djava.ext.dirs=%Axis_Lib%
set Output_Path=D:\workspace\test\src
set Package=server.SayHello
%Java_Cmd% org.apache.axis.wsdl.WSDL2Java -o%Output_Path% -p%Package% SayHello.wsdl
执行这个批处理文件就可以生成client stub.
生成的stub client文件列表为:SayHello.java,SayHelloService.java,SayHelloServiceLocator.java,SayHelloSoapBindingStub.java .
5、编写客户端程序,编译并执行
下面是一段junit测试客户端代码。
import java.net.URL;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
public class TestWSClient extends TestCase {
public TestWSClient(String string) {
super(string);
}
public void testSayHelloClient() throws Exception {
SayHelloService service = new SayHelloServiceLocator();
SayHello_PortType client = service.getSayHello() ;
String retValue = client.getName("jack");
assertEquals("hello jack", retValue);
}
}
三、SOAP监控
SOAP Monitor allows for the monitoring of SOAP requests and responses via
a web browser with Java plug-in 1.3 or higher. For a more comprehensive
explanation of its usage, read Using the
SOAP Monitor in the User's Guide.
By default, the SOAP Monitor is not enabled. The basic steps for enabling
it are compiling the SOAP Monitor java applet, deploying the SOAP Monitor
web service and adding request and response flow definitions for each monitored
web service. In more detail:
- Go to $AXIS_HOME/webapps/axis (or %AXIS_HOME%"webapps"axis)
and compile SOAPMonitorApplet.java.
On Windows
javac -classpath %AXIS_HOME%"lib"axis.jar SOAPMonitorApplet.java
On Unix
javac -classpath $AXIS_HOME/lib/axis.jar SOAPMonitorApplet.java
Copy all resulting class files (i.e. SOAPMonitorApplet*.class) to the root
directory of the web application using the SOAP Monitor
(e.g. .../tomcat/webapps/axis) 这一步axis做好了,不需要copy了。
- Deploy the SOAPMonitorService web service with the admin client and the
deploy-monitor.wsdd file (shown below).
Go to the directory deploy-monitor.wsdd is located and execute
the command below. The command assume that /axis is the intended
web application and it is available on port 8080.
On Windows
java -cp %AXISCLASSPATH%
org.apache.axis.client.AdminClient
-lhttp://localhost:8080/axis/services/AdminService deploy-monitor.wsdd
On UNIX
java -cp $AXISCLASSPATH
org.apache.axis.client.AdminClient
-lhttp://localhost:8080/axis/services/AdminService deploy-monitor.wsdd
SOAPMonitorService Deployment Descriptor (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>
- For each service that is to be monitored, add request and response flow definitions
to the service's deployment descriptor and deploy (or redeploy) the service. The
requestFlow and
responseFlow definitions follow the start tag of the
<service> element. If a service is already deployed, undeploy it and deploy
it with the modified deployment descriptor. An example is shown below:
...
<service name="xmltoday-delayed-quotes" provider="java:RPC">
<requestFlow>
<handler type="soapmonitor"/>
</requestFlow>
<responseFlow>
<handler type="soapmonitor"/>
</responseFlow>
...
- With a web browser, go to http[s]://host[:port][/webapp]/SOAPMonitor
(e.g. http://localhost:8080/axis/SOAPMonitor) substituting the correct values
for your web application. This will show the SOAP Monitor applet for viewing service
requests and responses. Any requests to services that have been configured
and deployed correctly should show up in the applet.进行上面的测试的时候,就可以看到request和response的内容。
四、自定义类型
如果返回类型为一个自定义类型:
import test.Person;
public class SayHello {
public Person getName(String name, int age) {
return new Person(name,age);
}
}
public class Person{
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
那么在发布的时候,就需要在发布文件中指明:
<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<service name="SayHello" provider="java:RPC">
<requestFlow>
<handler type="soapmonitor"/>
</requestFlow>
<responseFlow>
<handler type="soapmonitor"/>
</responseFlow>
<parameter name="className" value="SayHello"/>
<parameter name="allowedMethods" value="*"/>
</service>
<beanMapping qname="myNS:Person" xmlns:myNS="urn:SayHello" languageSpecificType="java:Person"/>
</deployment>
然后将Person类也copy到应用的classes文件下面,然后将wsdl文件下载下来,转化成java文件,这样调用的时候就可以直接得到Person对象了:
public void testSayHelloClient() throws Exception {
SayHelloService service = new SayHelloServiceLocator();
SayHello_PortType client = service.getSayHello() ;
test.Person person = client.getName("jack", 10);
assertEquals("jack", person.getName());
assertEquals(10, person.getAge());
}
五、Handlers and Chains
Now let's start to explore some of the more powerful features of the Axis
engine. Let's say you want to track how many times your service has been
called. We've included a sample handler in the samples/log directory to
do just this. To use a handler class like this, you first need to deploy
the Handler itself, and then use the name that you give it in deploying
a service. Here's a sample deploy.wsdd file (this is example 4 in samples/userguide):
1 <deployment xmlns="http://xml.apache.org/axis/wsdd/"
2
3 xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
4
5 <!-- define the logging handler configuration -->
6 <handler name="track" type="java:samples.userguide.example4.LogHandler">
7 <parameter name="filename" value="MyService.log"/>
8 </handler>
9 <!-- define the service, using the log handler we just defined -->
10 <service name="LogTestService"provider="java:RPC">
11 <requestFlow>
12 <handler type="track"/>
13 </requestFlow>
14 <parameter name="className" value="samples.userguide.example4.Service"/>
15 <parameter name="allowedMethods" value="*"/>
16 </service>
17 </deployment>
The first section defines a Handler called "track" that is implemented
by the class samples.userguide.example4.LogHandler. We give this Handler
an option to let it know which file to write its messages into.
Then we define a service, LogTestService, which is an RPC service just
like we saw above in our first example. The difference is the <requestFlow>
element inside the <service> - this indicates a set of Handlers that
should be invoked when the service is invoked, before the provider. By
inserting a reference to "track", we ensure that the message will be logged
each time this service is invoked.