技术开发 频道

MyEclipse下开发Web Service(Axis)

二、Quick Start环境准备好后,先从一个简单的例子开始。以便对使用Axis来开发Web services的大致流程有个了解。这个例子就是SayHello,请求端输入一个名字字符串,返回的将是一个问候语。

例如:输入了Tom,那么返回的事Hi,Tom.How are you?

2.1编写WSDL

A.启动MyEclipse,新建立一个WebApps(File->New->Project->Web Project),给Project Name 为SayHello,其他保持不改变。

B.选择File->New-Other菜单,进入后找到MyEclipse->Web Services并选择WSDL.

C. 选择“src”目录作为“Enter or select the parent folder”的值,“File name”值给定为SayHello.wsdl。点击“Next”进入下一步。



D.将目标命名空间设置为“http://ws.tonyzhangcn.org/SayHello/”.在生成代码的时候一般以这个命名空间为package的名字,例如:org.tonyzhangcn.ws.sayhello。这些值可以按需要设置。



E.点击“Finish”按钮就可以看到MyEclipse提供的WSDL 设计器的界面了。可以看到设计器为WSDL默认的添加了一个现操作。



即图中的“NewOperation”。现在将其改名为SayHello,如下图:



之后点击设计界面中的“Source”Tab来查看其代码。如下:可以看到我们定义了一个SayHello的Web Services,她提供了一个SayHello的方法,她能够接受一个String(事实上是tns:SayHelloRequest对象,她对String进行了封装)类型的输入参数SayHelloRequest并返回一个String(事实上是tns:SayHelloResponse对象,她对String进行了封装)类型的SayHelloResponse结果。更多关于WSDL的信息,请参考W3C的规范文档。

<?xml version="1.0" encoding="UTF-8"?> <wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns=
"http://ws.tonyzhangcn.org/SayHello/"
xmlns:wsdl=http://schemas.xmlsoap.org/wsdl/
xmlns:xsd
="http://www.w3.org/2001/XMLSchema" name="SayHello"
targetNamespace
="http://ws.tonyzhangcn.org/SayHello/"> <wsdl:types> <xsd:schema targetNamespace="http://ws.tonyzhangcn.org/SayHello/"> <xsd:element name="SayHelloResponse" type="xsd:string" /> <xsd:element name="SayHelloRequest" type="xsd:string" /> </xsd:schema> </wsdl:types> <wsdl:message name="SayHelloResponse"> <wsdl:part element="tns:SayHelloResponse" name="SayHelloResponse" /> </wsdl:message> <wsdl:message name="SayHelloRequest"> <wsdl:part element="tns:SayHelloRequest" name="SayHelloRequest" /> </wsdl:message> <wsdl:portType name="SayHello"> <wsdl:operation name="SayHello"> <wsdl:input message="tns:SayHelloRequest" /> <wsdl:output message="tns:SayHelloResponse" /> </wsdl:operation> </wsdl:portType> <wsdl:binding name="SayHelloSOAP" type="tns:SayHello"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" /> <wsdl:operation name="SayHello"> <soap:operation soapAction="http://ws.tonyzhangcn.org/SayHello/NewOperation" /> <wsdl:input> <soap:body use="literal" /> </wsdl:input> <wsdl:output> <soap:body use="literal" /> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="SayHello"> <wsdl:port binding="tns:SayHelloSOAP" name="SayHelloSOAP"> <soap:address location="http://www.example.org/" /> </wsdl:port> </wsdl:service> </wsdl:definitions>

至此WSDL的编写已经完成。

0
相关文章