安裝Axis和Tomcat
tomcat的安裝就不細(xì)說(shuō)了,將解壓的axis中的webapps目錄下的axis拷貝到tomcat安裝路徑下的webapp下,將解壓的axis下lib下的jar文件拷貝到tomcat安裝目錄下common\lib\下,并把他們加入到你的系統(tǒng)路徑中。應(yīng)該就沒(méi)什么問(wèn)題了,好,現(xiàn)在可以來(lái)開(kāi)發(fā)你的web服務(wù)了。
部署web服務(wù)
在axis下部署web服務(wù)有以下兩種方式:
1.即時(shí)部署(Instance Deployment)?D?D利用JWS文件
只需要將.java文件拷貝到axis的app目錄下,并將文件后綴改為.jws即可。
訪問(wèn)部署后的wsdl文件只需鍵入:
http://localhost:8080/axis/filename.jws?wsdl ;
2.定制部署(Custom Deployment)?D?D利用部署描述符wsdd
以下是部署描述符的一個(gè)例子:
<deployment xmlns="http://xml.apache.org/axis/wsdd/"
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<service name="MyService" provider="java:RPC">
<parameter name="className" value="samples.userguide.example3.MyService"/>
<parameter name="allowedMethods" value="*"/>
</service>
</deployment>
有了這個(gè)文件后,我們就可以利用AdminClient來(lái)部署web服務(wù)
如果你已經(jīng)將axis部署在自己的web服務(wù)器上,如Tomcat,且已經(jīng)將所需的.jar文件加入到了系統(tǒng)路徑中,如axis.jar, commons-discovery.jar, commons-logging.jar, jaxrpc.jar, saaj.jar, log4j-1.2.4.jar等。則可以利用AdminClient來(lái)對(duì)wsdd文件部署,采用以下命令形式:
>java org.apache.axis.client.AdminClient deploy.wsdd
默認(rèn)的端口是8080,如果采用了其它的服務(wù)器,則運(yùn)行上述命令時(shí)應(yīng)該用-p參數(shù)。
若成功,應(yīng)出現(xiàn):
<Admin>Done Processing</Admin>
現(xiàn)在我們就可以通過(guò)SOAP調(diào)用這個(gè)服務(wù)了,可以運(yùn)行客戶程序來(lái)驗(yàn)證它,如下:
>java sample.client ?Clhttp://localhost:8080/axis/services/Myservice “test me!”
XML和Java數(shù)據(jù)類型之間的映射
互操作性是SOAP實(shí)現(xiàn)的一個(gè)很大的挑戰(zhàn),如果要使得你的服務(wù)在不同的平臺(tái)之間運(yùn)行,必須正視這個(gè)問(wèn)題。
從wsdl到j(luò)ava的標(biāo)準(zhǔn)映射:
xsd:base64Binary byte[]
xsd:base64Binary byte[]
xsd:boolean boolean
xsd:byte byte
xsd:dateTime java.util.Calendar
xsd:decimal java.math.BigDecimal
xsd:double double
xsd:float float
xsd:hexBinary byte[]
xsd:int int
xsd:integer java.math.BigInteger
xsd:long long
xsd:QName javax.xml.namespace.QName
xsd:short short
xsd:string java.lang.String
在axis中應(yīng)用wsdl
wsdl是IBM和微軟開(kāi)發(fā)的一種規(guī)范,許多廠商都支持它。一個(gè)服務(wù)的wsdl描述展示了以下一些內(nèi)容:以一個(gè)機(jī)器可以理解的形式提供了服務(wù)的接口、服務(wù)所用到的數(shù)據(jù)類型、以及定位服務(wù)的地址等。Axis支持3種wsdl的用法:
1. 利用?wsdl來(lái)查看wsdl文件。在你部署好一個(gè)服務(wù)后,可以在它的url后加上?wsdl來(lái)查看他的wsdl文件。
2. Axis提供了”WSDL2Java”工具,可以利用wsdl描述來(lái)產(chǎn)生服務(wù)的Java代理和框架(proxy and skeletons)。
3. Axis提供了”Java2WSDL”工具,可以由java類生成wsdl文件。
下面我們主要來(lái)討論一下WSDL2Java 和 Java2WSDL
WSDL2Java:由WSDL文件生成存根、框架、數(shù)據(jù)類型(stubs, skeletons, data types)。
該工具的調(diào)用方式如下:
客戶端綁定
>java org.apache.axis.wsdl.WSDL2Java wsdl-file-url
生成的文件會(huì)放在對(duì)應(yīng)的文件目錄中,這個(gè)目錄是由wsdl文件中的目標(biāo)名稱空間映射的。
WSDL clause Java class(es) generated
For each entry in the type section A java class
A holder if this type is used as an inout/out parameter
For each portType A java interface
For each binding A stub class
For each service A service interface
A service implementation (the locator)
服務(wù)端綁定
>java org.apache.axis.wsdl.WSDL2Java --server-side --skeletonDeploy true file.wsdl
使用這個(gè)選項(xiàng),會(huì)額外產(chǎn)生幾個(gè)新類,如下:
WSDL clause Java class(es) generated
For each binding A skeleton class
An implementation template class
For all services One deploy.wsdd file
One undeploy.wsdd file
如果不使用--skeletonDeploy true選項(xiàng),skeleton類不會(huì)產(chǎn)生。而產(chǎn)生的wsdd文件則會(huì)表明服務(wù)實(shí)現(xiàn)是直接被部署的。
Java2WSDL 由Java類生成WSDL
其用法在下面的例子中已經(jīng)提到,這里就不再贅述。
開(kāi)發(fā)示例
WSDL2Java和Java2WSDL使得開(kāi)發(fā)新的web服務(wù)特別簡(jiǎn)單。以下展示了開(kāi)發(fā)一個(gè)新的服務(wù)的步驟:
提供一個(gè)接口或Java類;
使用Java2WSDL生成WSDL;
例子:
>java org.apache.axis.wsdl.Java2WSDL ?Co wp.wsdl ?Cl”http://localhost:8080/axis/
services/WidgetPrice” ?Cn "urn:Example6" -p"samples.userguide.example6”
"urn:Example6" samples.userguide.example6.WidgetPrice
其中的選項(xiàng)請(qǐng)參考附錄
使用WSDL2Java生成綁定。
例子:
>java org.apache.axis.wsdl.WSDL2Java -o . -d Session -s -S true -Nurn:Example6 samples.userguide.example6 wp.wsdl
其中的選項(xiàng)請(qǐng)參考附錄
這會(huì)產(chǎn)生以下類:
WidgetPriceSoapBindingImpl.java
WidgetPrice.java 這個(gè)類包含了java.rmi.Remote的用法
WidgetPriceService.java
WidgetPriceServiceLocator.java
WidgetPriceSoapBindingSkeleton.java
WidgetPriceSoapBindingStub.java
deploy.wsdd
undeploy.wsdd
datatype.java
這里需要注意的是,生成這些文件后,WidgetPrice是重新生成的包含了rmi.Remote接口的新類,你的web服務(wù)的實(shí)現(xiàn)需要在WidgetPriceSoapBindingImpl.java 類中重新實(shí)現(xiàn)(也就是要修改它的代碼,把其中的空的方法具體實(shí)現(xiàn)),然后編譯,將生成的類文件拷貝到%tomcat_home%\webapp\axis\WEB-INF\classes\你的項(xiàng)目包\類文件。自己再寫(xiě)一個(gè)簡(jiǎn)單的客戶端,或它本身在WSDL2Java的時(shí)候也可以生成testcase(加-t選項(xiàng)),axis本身帶的例子可以用來(lái)參考。這樣就可以調(diào)用你的web服務(wù)了,當(dāng)然客戶端也可以開(kāi)發(fā)成web頁(yè)面的形式等等。
作者:hesan
附錄:
Java2WSDL emitter
Usage: java org.apache.axis.wsdl.Java2WSDL [options] class-of-portType
Options:
-h, --help print this message and exit
-I, --input <argument> input WSDL filename
-o, --output <argument> output WSDL filename
-l, --location <argument> service location url
-P, --portTypeName <argument> portType name (obtained from class-of-portType if not specified)
-b, --bindingName <argument>
binding name ((--servicePortName value + "SOAPBinding" if not specified)
-S, --serviceElementName <argument>
service element name (defaults to servicePortName value + "Service")
-s, --servicePortName <argument>
service port name (obtained from --location if not specified)
-n, --namespace <argument> target namespace
-p, --PkgtoNS <argument>=<value>package=namespace, name value pairs
-m, --methods <argument>
space or comma separated list of methods to export
-a, --all
look for allowed methods in inherited class
-w, --outputWsdlMode <argument>
output WSDL mode: All, Interface, Implementation
-L, --locationImport <argument> location of interface WSDL
-N, --namespaceImpl <argument>
target namespace for implementation WSDL
-O, --outputImpl <argument>
output Implementation WSDL filename, setting this causes
--outputWsdlMode to be ignored
-i, --implClass <argument>
optional class that contains implementation of methods in
class-of-portType. The debug information in the class is used to obtain the method parameter names, which are used to set the WSDL part names.
-x, --exclude <argument>
space or comma separated list of methods not to export
-y, --style <argument>
the style of the wsdl document: RPC, DOCUMENT or WRAPPED
-c, --stopClasses <argument>
space or comma separated list of class names which stop inheritance search if --all switch is enabled
-T, --typeMappingVersion <argument>
indicate 1.1 or 1.2. The default is 1.2 (SOAP 1.2 JAX-RPC compliant)
-A, --soapAction <argument>
value of the operations soapAction field. Values are DEFAULT, OPERATION or NONE. OPERATION forces soapAction to the name of the operation. DEFAULT causes the soapAction to be set according to the operations meta data (usually ""). NONE forces the soapAction to "". The default is DEFAULT.
-y, --style <argument>
the style of the wsdl document: RPC, DOCUMENT or WRAPPED
Details: ortType element name= <.portTypeName value> OR <class-of-portType name> binding element name= <--bindingName value> OR <--servicePortName value>SoapBinding service element name= <--serviceElementName value> OR <portTypeName value>Service
port element name= <--servicePortName value>
address location = <--location value>
WSDL2Java
Usage: java org.apache.axis.wsdl.WSDL2Java [options] WSDL-URI
Options:
-h, --help
print this message and exit
-v, --verbose
print informational messages
-n, --noImports
only generate code for the immediate WSDL document
-O, --timeout <argument>
timeout in seconds (default is 45, specify -1 to disable)
-D, --Debug
print debug information
-W, --noWrapped
turn off support for "wrapped" document/literal
-s, --server-side
emit server-side bindings for web service
-S, --skeletonDeploy <argument>
deploy skeleton (true) or implementation (false) in deploy.wsdd.
Default is false. Assumes --server-side.
-N, --NStoPkg <argument>=<value>
mapping of namespace to package
-f, --fileNStoPkg <argument>
file of NStoPkg mappings (default NStoPkg.properties)
-p, --package <argument>
override all namespace to package mappings, use this package
name instead
-o, --output <argument>
output directory for emitted files
-d, --deployScope <argument>
add scope to deploy.xml: "Application", "Request", "Session"
-t, --testCase
emit junit testcase class for web service
-a, --all
generate code for all elements, even unreferenced ones
-T, --typeMappingVersion
indicate 1.1 or 1.2. The default is 1.1 (SOAP 1.1 JAX-RPC compliant. 1.2 indicates SOAP 1.1 encoded.)
-F, --factory <argument>
name of a custom class that implements GeneratorFactory interface (for extending Java generation functions)
-H, --helperGen
emits separate Helper classes for meta data
-U, --user <argument>
username to access the WSDL-URI
-P, --password <argument>
password to access the WSDL-URI