前段時(shí)間,系統(tǒng)需要對(duì)外發(fā)布web service接口,采用的是Axis,主要是參考網(wǎng)上的一篇文章,mornitor和自定義類型是自己加上的:
一、Axis環(huán)境的安裝
1、安裝環(huán)境 J2SE SDK,Tomcat,eclipse。
2、到 http://xml.apache.org 網(wǎng)站下載Axis安裝包。
3、將Axis相關(guān)包文件放在WEB-INF\lib目錄下,需要另外下載activation.jar。
Axis支持三種web service的部署和開(kāi)發(fā),分別為:
1、Dynamic Invocation Interface (DII)
2、Dynamic Proxy方式
3、Stubs方式
前兩種方式我就不介紹了,同事告訴我他們自己都不使用前兩種方式,他們建議我們使用Stubs方式,因此我就主要就介紹一下第三種方式。注意,我自己的Java源代碼是放在D:\workspace\test\目錄下,Axis相關(guān)包文件放在D:\workspace\test\WEB-INF目錄下。
二、編寫wsdd發(fā)布web服務(wù),編寫stub client訪問(wèn)web服務(wù)
1、編寫服務(wù)端程序server,SayHello.java,編譯server.SayHello.java
package server;
public class SayHello
{
public String getName(String name)
{
return "hello "+name;
}
}
2、編寫wsdd文件
deploy.wsdd文件內(nèi)容如下:
<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、發(fā)布服務(wù):
編輯一個(gè)deploy.bat,Axis_Lib為axis.jar路徑。內(nèi)容如下(注意目錄名中不要包含空格):
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
執(zhí)行這個(gè)批處理文件,執(zhí)行這個(gè)批處理之前,tomcat必須啟動(dòng)起來(lái)。如果處理成功的話,然后將class文件拷貝到axis的axis"WEB-INF"classes目錄中,然后訪問(wèn)http://localhost:8080/test/services 就會(huì)顯示服務(wù)列表。
4、生成客戶端client stub文件
在瀏覽器上訪問(wèn)服務(wù)器端的服務(wù),可以下載到WSDL文件,通過(guò)Axis的相關(guān)工具,可以自動(dòng)從WSDL文件中生成Web Service的客戶端代碼。
編寫一個(gè)WSDL2Java.bat文件,其內(nèi)容如下:
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
執(zhí)行這個(gè)批處理文件就可以生成client stub.
生成的stub client文件列表為:SayHello.java,SayHelloService.java,SayHelloServiceLocator.java,SayHelloSoapBindingStub.java .
5、編寫客戶端程序,編譯并執(zhí)行
下面是一段junit測(cè)試客戶端代碼。
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監(jiān)控
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.進(jìn)行上面的測(cè)試的時(shí)候,就可以看到request和response的內(nèi)容。
四、自定義類型
如果返回類型為一個(gè)自定義類型:
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;
}
}
那么在發(fā)布的時(shí)候,就需要在發(fā)布文件中指明:
<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到應(yīng)用的classes文件下面,然后將wsdl文件下載下來(lái),轉(zhuǎn)化成java文件,這樣調(diào)用的時(shí)候就可以直接得到Person對(duì)象了:
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.