在axis中应用java2wsdl

 

http://www.360doc.com/showWeb/0/12/5950.aspx
tomcat的安装就不细说了,将解压的axis中的webapps目录下的axis拷贝到tomcat安装路径下的webapp下,将解压的axis下 lib下的jar文件拷贝到tomcat安装目录下commonlib下,并把他们加入到你的系统路径中。应该就没什么问题了,好,现在可以来开发你的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

以下是部署描述符的一个例子:
<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>

有了这个文件后,我们就可以利用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参数。
若成功,应出现:
<Admin>Done Processing</Admin>
现在我们就可以通过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%webappaxisWEB-INFclasses你的项目包类文件。自己再写一个简单的客户端,或它本身在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 <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
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JavaAxis2作为两种流行的编程语言和网络服务框架,方便开发人员创建和使用Web服务应用程序。在Java应用程序,调用Web服务的一种有效方式是使用Axis2框架。以下是使用Java Axis2调用Web服务接口的步骤: 首先,你需要创建一个Java项目并添加Axis2依赖项。在Eclipse或其他Java IDE,你可以右键单击项目并选择“属性”>“Java构建路径”>“库”>“添加外部JAR文件”将Axis2 JAR包添加到项目。 然后,你需要使用WSDLWeb服务描述语言)文件创建客户端代码。WSDL文件描述了Web服务接口的各个方法及其参数和返回值。Java Axis2提供了一个工具,可以根据WSDL文件自动生成客户端代码。你可以使用cmd命令行窗口,转到Axis2的bin目录并执行以下命令创建客户端代码: wsdl2java -uri <WSDL文件路径> -s -d <输出目录路径> 执行完命令后,生成的客户端代码将保存在指定的输出目录路径。 最后,在Java应用程序,你需要创建一个服务客户端对象,使用生成的客户端代码调用Web服务的方法。以下是使用Java Axis2调用Web服务接口的示例: 1.创建服务客户端对象 Service service = new ServiceClient(); 2.创建服务端点对象 EndpointReference endpoint = new EndpointReference(<Web服务的URL>); 3.创建调用操作对象 Options options = new Options(); options.setTo(endpoint); options.setAction(<Web服务的命名空间和操作名称>); 4.调用Web服务的方法 OMElement result = service.invokeBlocking(<Web服务的方法名称>, <Web服务的参数>, <Web服务的参数类型>); 以上步骤可以使Java应用程序成功调用Web服务接口,从而实现Web服务的功能。但在实际应用,还需要考虑安全性、性能优化等方面的因素。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值