用
AXIS
開發(fā)
WEBSERVICE
的兩種部署方式的簡單介紹
WEBSERVICE
有兩種部署方式:
SOAP Endpoint
和
JWS Endpoint
。
JWS endpoint
的部署方式是一中最簡單的部署方式
:
1.
?
寫一個服務(wù)端程序:
/**
?*
該服務(wù)是輸入你的名字和性別,然后返回信息。
?*/
public class HelloServicesForJws {
??? /**
???? *
???? * @param name your name
???? * @param sex if you are a man then the sex==true;otherwise sex==false
???? * @return
???? */
??? public String echoString(String name,boolean sex) {
??????? if(sex) {
??????????? return "Hello Mr "+name+"! Welcome to WebService Word!";
??????? } else {
??????????? return "Hello Mrs "+name+"! Welcome to WebService Word!";
??????? }
??? }
}
2.
?
在
TOMCAT
上以
JWS
的形式部署該服務(wù):將這個文件(
HelloServicesForJws.java
)拷貝到
webapps/
axis/
目錄下,并把這個文件的名字改為
HelloServicesForJws.jws
。這個文件可以拷貝到
webapps/
axis/
的任意目錄下,但是,不要放在
WEB-INF
子目錄下。
3.
?
啟動
TOMCAT
,測試部署成功與否。訪問地址是:
如果
HelloServicesForJws.jws
直接放在
webapps/
axis/
目錄下用
http://localhost:8080/axis/HelloServicesForJws.jws
如在其他的目錄
(training)
下用:
http://localhost:8080/axis/training/HelloServicesForJws.jws
4.
?
如果測試成功,我們可以在
jwsClasses
目錄下可以看到
HelloServicesForJws.class
文件。
5.
?
注意:當(dāng)以這種形式部署的時候,
JAVA
文件不能放在一個包里面。
?
SOAP Endpoint
部署方式:
1.
?
同樣我們一上面的程序為列,但是,我門下面的一個包中,文件名也稍微改一下。如下所示:
package com.unimas.datacollection.webservices.training;
public class HelloServicewsForSoap {
??? /**
???? * @param name your name
???? * @param sex if you are a man then the sex==true;otherwise sex==false
???? * @return
???? */
??? public String echoString(String name,boolean sex) {
??????? if(sex) {
??????????? return "Hello Mr "+name+"! Welcome to WebService Word!";
??????? } else {
??????????? return "Hello Mrs "+name+"! Welcome to WebService Word!";
??????? }
??? }
}
2.
?
然后將這個文件打成一個包:
training.jar
3.
?
將這個包拷貝到
WEB-INF/lib
目錄下。
4.
?
修改文件
Server-config.wsdd
。在文件中添加下列內(nèi)容:
?<service name="HelloServices" provider="java:RPC">
<parameter name="allowedMethods" value="*"/>
<parameter name="className" value="com.unimas.datacollection.webservices.training.HelloServicewsForSoap"/>
</service>
我們也可以不用手工來修改
Server-config.wsdd
。我們可以使用
AdminClient
工具在
Server-config.wsdd
文件中輕松的部署上我們需要的服務(wù),或是停止某一個服務(wù)。步驟如下:
1.
?
新建文件:
deploy_helloService.wsdd
,和
undeploy_helloService.wsdd
。文件內(nèi)容如下:
Deploy-helloservices.wsdd
內(nèi)容:
<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
??? <service name="HelloServices" provider="java:RPC">
??????? <parameter name="allowedMethods" value="*"/>
??????? <parameter name="className" value="com.unimas.datacollection.webservices.training.HelloServicewsForSoap"/>
??? </service>
</deployment>
undeploy-helloservices.wsdd
內(nèi)容:
<undeployment name="test" xmlns="http://xml.apache.org/axis/wsdd/">
? <service name="HelloServices"/>
</undeployment>
2.
?
運行
AdminClient.main(new String[]{deployFile});
可以部署或是停止服務(wù)。
5.
?
啟動
TOMCAT
。
6.
?
用
IE
訪問測試:
http://localhost:8080/axis/services/ HelloServices
以下列地址訪問
WSDL
文件:
http://localhost:8080/axis/services/ HelloServices
?
wsdl
?
Service Styles
AXIS
有四種
service styles
,分別是:
RPC, Document, Wrapped, and Message
。最常用的就是
RPC
和
Message
。
RPC
:
在
AXIS
中是一個默認選項。當(dāng)你部署的時候使用下列兩種方式:
<service ... provider="java:RPC">
或則 <service ... style="RPC">,它遵循SOAP RPC和編碼規(guī)則,就是說它的WSDL文件形式就想上面所說的
HelloServicewsForSoap
的
echoString
一樣。每個
RPC
都包括一個表示名稱的外部接點和一些表示參數(shù)的內(nèi)部接點。
AXIS
會根據(jù)規(guī)則將一個
XML
(
WSDL
文件)文件轉(zhuǎn)化成一個
JAVA
對象,并對對想賦上在文件中描述的值。也可以根據(jù)規(guī)則將一個
JAVA
對象轉(zhuǎn)化成
XML
文件。
Document
以下列方式部署:
<service ... style="document"><service ... style="document">
。適合于老的XML schema。
Wrapped
以下列方式部署:
<service ... style="wrapped"><service ... style="wrapped">
和DOCUMENT一樣,適合于老的XML schema。
在大多書情況下,你不許要擔(dān)心是DOCUMENT服務(wù)還是WRAPPED服務(wù)。
Message
以下列方式部署:
<service name="MessageService" style="message">
。以這種方式部署的話,會是AXIS失去意義,它使你的代碼真正的用XML形式,而不需要轉(zhuǎn)化成JAVA對象。以這種方式部署的有以下四種服務(wù)方法:
public Element [] method(Element [] bodies);
public SOAPBodyElement [] method (SOAPBodyElement [] bodies);
public Document method(Document body);
public void method(SOAPEnvelope req, SOAPEnvelope resp);
?