用tomcat和axis开发web服务

来源:百度文库 编辑:神马文学网 时间:2024/03/28 22:12:54
用tomcat和axis开发web服务!
Ken·Ivan
安装Axis和Tomcat
tomcat的安装就不细说了,将解压的axis中的webapps目录下的axis拷贝到tomcat安装路径下的webapp下,将解压的axis下lib下的jar文件拷贝到tomcat安装目录下common\lib\下,并把他们加入到你的系统路径中。应该就没什么问题了,好,现在可以来开发你的web服务了。
部署web服务
在axis下部署web服务有以下两种方式:
1.即时部署(Instance Deployment)?D?D利用JWS文件
只需要将.java文件拷贝到axis的app目录下,并将文件后缀改为.jws即可。
访问部署后的wsdl文件只需键入:
http://localhost:8080/axis/filename.jws?wsdl ;
2.定制部署(Custom Deployment)?D?D利用部署描述符wsdd
以下是部署描述符的一个例子:
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">





有了这个文件后,我们就可以利用AdminClient来部署web服务
如果你已经将axis部署在自己的web服务器上,如Tomcat,且已经将所需的.jar文件加入到了系统路径中,如axis.jar, commons-discovery.jar, commons-logging.jar, jaxrpc.jar, saaj.jar, log4j-1.2.4.jar等。则可以利用AdminClient来对wsdd文件部署,采用以下命令形式:
>java org.apache.axis.client.AdminClient deploy.wsdd
默认的端口是8080,如果采用了其它的服务器,则运行上述命令时应该用-p参数。
若成功,应出现:
Done Processing
现在我们就可以通过SOAP调用这个服务了,可以运行客户程序来验证它,如下:
>java sample.client ?Clhttp://localhost:8080/axis/services/Myservice “test me!”
XML和Java数据类型之间的映射
互操作性是SOAP实现的一个很大的挑战,如果要使得你的服务在不同的平台之间运行,必须正视这个问题。
从wsdl到java的标准映射:
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中应用wsdl
wsdl是IBM和微软开发的一种规范,许多厂商都支持它。一个服务的wsdl描述展示了以下一些内容:以一个机器可以理解的形式提供了服务的接口、服务所用到的数据类型、以及定位服务的地址等。Axis支持3种wsdl的用法:
1. 利用?wsdl来查看wsdl文件。在你部署好一个服务后,可以在它的url后加上?wsdl来查看他的wsdl文件。
2. Axis提供了”WSDL2Java”工具,可以利用wsdl描述来产生服务的Java代理和框架(proxy and skeletons)。
3. Axis提供了”Java2WSDL”工具,可以由java类生成wsdl文件。
下面我们主要来讨论一下WSDL2Java 和 Java2WSDL
 WSDL2Java:由WSDL文件生成存根、框架、数据类型(stubs, skeletons, data types)。
该工具的调用方式如下:
 客户端绑定
>java org.apache.axis.wsdl.WSDL2Java wsdl-file-url
生成的文件会放在对应的文件目录中,这个目录是由wsdl文件中的目标名称空间映射的。
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)
 服务端绑定
>java org.apache.axis.wsdl.WSDL2Java --server-side --skeletonDeploy true file.wsdl
使用这个选项,会额外产生几个新类,如下:
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选项,skeleton类不会产生。而产生的wsdd文件则会表明服务实现是直接被部署的。
 Java2WSDL 由Java类生成WSDL
其用法在下面的例子中已经提到,这里就不再赘述。
开发示例
WSDL2Java和Java2WSDL使得开发新的web服务特别简单。以下展示了开发一个新的服务的步骤:
 提供一个接口或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
其中的选项请参考附录
 使用WSDL2Java生成绑定。
例子:
>java org.apache.axis.wsdl.WSDL2Java -o . -d Session -s -S true -Nurn:Example6 samples.userguide.example6 wp.wsdl
其中的选项请参考附录
这会产生以下类:
WidgetPriceSoapBindingImpl.java
WidgetPrice.java 这个类包含了java.rmi.Remote的用法
WidgetPriceService.java
WidgetPriceServiceLocator.java
WidgetPriceSoapBindingSkeleton.java
WidgetPriceSoapBindingStub.java
deploy.wsdd
undeploy.wsdd
datatype.java
这里需要注意的是,生成这些文件后,WidgetPrice是重新生成的包含了rmi.Remote接口的新类,你的web服务的实现需要在WidgetPriceSoapBindingImpl.java 类中重新实现(也就是要修改它的代码,把其中的空的方法具体实现),然后编译,将生成的类文件拷贝到%tomcat_home%\webapp\axis\WEB-INF\classes\你的项目包\类文件。自己再写一个简单的客户端,或它本身在WSDL2Java的时候也可以生成testcase(加-t选项),axis本身带的例子可以用来参考。这样就可以调用你的web服务了,当然客户端也可以开发成web页面的形式等等。
作者:hesan
附录:
Java2WSDL emitter
Usage: java org.apache.axis.wsdl.Java2WSDL [options] class-of-portType
Options:
-h, --help print this message and exit
-I, --input  input WSDL filename
-o, --output  output WSDL filename
-l, --location  service location url
-P, --portTypeName  portType name (obtained from class-of-portType if not specified)
-b, --bindingName 
binding name ((--servicePortName value + "SOAPBinding" if not specified)
-S, --serviceElementName 
service element name (defaults to servicePortName value + "Service")
-s, --servicePortName 
service port name (obtained from --location if not specified)
-n, --namespace  target namespace
-p, --PkgtoNS =package=namespace, name value pairs
-m, --methods 
space or comma separated list of methods to export
-a, --all
look for allowed methods in inherited class
-w, --outputWsdlMode 
output WSDL mode: All, Interface, Implementation
-L, --locationImport  location of interface WSDL
-N, --namespaceImpl 
target namespace for implementation WSDL
-O, --outputImpl 
output Implementation WSDL filename, setting this causes
--outputWsdlMode to be ignored
-i, --implClass 
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 
space or comma separated list of methods not to export
-y, --style 
the style of the wsdl document: RPC, DOCUMENT or WRAPPED
-c, --stopClasses 
space or comma separated list of class names which stop inheritance search if --all switch is enabled
-T, --typeMappingVersion 
indicate 1.1 or 1.2. The default is 1.2 (SOAP 1.2 JAX-RPC compliant)
-A, --soapAction 
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 
the style of the wsdl document: RPC, DOCUMENT or WRAPPED
Details: ortType element name= <.portTypeName value> OR  binding element name= <--bindingName value> OR <--servicePortName value>SoapBinding service element name= <--serviceElementName value> OR 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 
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 
deploy skeleton (true) or implementation (false) in deploy.wsdd.
Default is false. Assumes --server-side.
-N, --NStoPkg =
mapping of namespace to package
-f, --fileNStoPkg 
file of NStoPkg mappings (default NStoPkg.properties)
-p, --package 
override all namespace to package mappings, use this package
name instead
-o, --output 
output directory for emitted files
-d, --deployScope 
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 
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 
username to access the WSDL-URI
-P, --password 
password to access the WSDL-URI