用Tuscany、Axis、groovy来发布和调试Web Service

Tuscany是一个符合SCA标准的开源实现,他能够很容易地将一个服务绑定为一个Web Service:
<composite name="Employee" xmlns="http://www.osoa.org/xmlns/sca/1.0"><service name="HelloWorldService"><interface.wsdl><component name="HelloWorldServiceComponent"><implementation.java class="helloworld.HelloWorldImpl">
xml 代码
 
  1. <composite xmlns="http://www.osoa.org/xmlns/sca/1.0" name="Employee">  
  2.     <service name="HelloWorldService"  
  3.         promote="HelloWorldServiceComponent">  
  4.         <interface.wsdl  
  5.             interface="http://helloworld#wsdl.interface(HelloWorld)" />  
  6.         <binding.ws uri="http://localhost:8085/HelloWorldService" />  
  7.     </service>  
  8.     <component name="HelloWorldServiceComponent">  
  9.         <implementation.java class="helloworld.HelloWorldImpl" />  
  10.     </component>  
  11. </composite>  
</component>
要使服务发布成功,要保证有以下几个文件:
</interface.wsdl></service></composite>
  1. 一个预定义的wsdl文件,只要保证这个文件在classpath下就可以了。
  2. 一个java接口。
  3. 一个java类。
下面列依次列出这几个文件:
* helloworld.wsdl
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://helloworld" targetnamespace="http://helloworld">
xml 代码
 
  1. <wsdl:definitions targetNamespace="http://helloworld" xmlns:tns="http://helloworld" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"  
  2.     name="helloworld">  
  3.     <wsdl:types>  
  4.         <schema elementFormDefault="qualified" targetNamespace="http://helloworld" xmlns="http://www.w3.org/2001/XMLSchema">  
  5.             <element name="getGreetings">  
  6.                 <complexType>  
  7.                     <sequence>  
  8.                         <element name="name" type="xsd:string"/>  
  9.                     </sequence>  
  10.                 </complexType>  
  11.             </element>  
  12.   
  13.             <element name="getGreetingsResponse">  
  14.                 <complexType>  
  15.                     <sequence>  
  16.                         <element name="getGreetingsReturn" type="xsd:string"/>  
  17.                     </sequence>  
  18.                 </complexType>  
  19.             </element>  
  20.             
  21.         </schema>  
  22.     </wsdl:types>  
  23.     <wsdl:message name="getGreetingsRequest">  
  24.         <wsdl:part element="tns:getGreetings" name="parameters"/>  
  25.     </wsdl:message>  
  26.     <wsdl:message name="getGreetingsResponse">  
  27.         <wsdl:part element="tns:getGreetingsResponse" name="parameters"/>  
  28.     </wsdl:message>  
  29.   
  30.     <wsdl:portType name="HelloWorld">  
  31.         <wsdl:operation name="getGreetings">  
  32.             <wsdl:input message="tns:getGreetingsRequest" name="getGreetingsRequest"/>  
  33.             <wsdl:output message="tns:getGreetingsResponse" name="getGreetingsResponse"/>  
  34.         </wsdl:operation>  
  35.     </wsdl:portType>  
  36.   
  37.     <wsdl:binding name="HelloWorldSoapBinding" type="tns:HelloWorld">  
  38.         <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>  
  39.         <wsdl:operation name="getGreetings">  
  40.             <wsdlsoap:operation soapAction=""/>  
  41.             <wsdl:input name="getGreetingsRequest">  
  42.                 <wsdlsoap:body use="literal"/>  
  43.             </wsdl:input>  
  44.             <wsdl:output name="getGreetingsResponse">  
  45.                 <wsdlsoap:body use="literal"/>  
  46.             </wsdl:output>  
  47.         </wsdl:operation>  
  48.     </wsdl:binding>  
  49.   
  50.     <wsdl:service name="HelloWorldService">  
  51.         <wsdl:port binding="tns:HelloWorldSoapBinding" name="HelloWorldSoapPort">  
  52.             <wsdlsoap:address location="http://localhost:8085/HelloWorldService"/>  
  53.         </wsdl:port>  
  54.     </wsdl:service>  
  55.   
  56. </wsdl:definitions>  
<wsdl:types><schema xmlns="http://www.w3.org/2001/XMLSchema" targetnamespace="http://helloworld" elementformdefault="qualified"><wsdl:message name="getGreetingsRequest"><wsdl:message name="getGreetingsResponse"><wsdl:porttype name="HelloWorld"><wsdl:operation name="getGreetings"><wsdl:binding type="tns:HelloWorld" name="HelloWorldSoapBinding"><wsdlsoap:binding transport="http://schemas.xmlsoap.org/soap/http" style=""><wsdl:operation name="getGreetings"><wsdlsoap:operation soapaction="">
* HelloWorldService.java
java 代码
 
  1. package helloworld;  
  2. import org.osoa.sca.annotations.Remotable;  
  3. @Remotable  
  4. public interface HelloWorldService {  
  5.     public String getGreetings(String name);  
  6. }  


* HelloWorldImpl.java
java 代码
 
  1. package helloworld;  
  2. import org.osoa.sca.annotations.Service;  
  3. @Service(HelloWorldService.class)  
  4. public class HelloWorldImpl implements HelloWorldService {  
  5.     public String getGreetings(String name) {  
  6.         return "hello "+name;  
  7.     }  
  8. }  


这样我们就可以写一个类来创建一个SCA中的Domain,并由它来创建组件并发布为一个web服务。
java 代码
 
  1. public class HelloWorldServer {  
  2.     public static void main(String[] args) {  
  3.         SCADomain scaDomain = SCADomain.newInstance("","","Calculator.composite","Employee.composite");  
  4.         try {  
  5.             System.out.println("HelloWorld server started (press enter to shutdown)");  
  6.             System.in.read();  
  7.         } catch (IOException e) {  
  8.             e.printStackTrace();  
  9.         }  
  10.         scaDomain.close();  
  11.         System.out.println("HelloWorld server stopped");  
  12.     }  
  13. }  

运行这个类,也就启动了一个SCA的Domain,打开浏览器输入http://localhost:8085/HelloWorldService? wsdl(注意这个是以binding.ws的uri为准的,和wsdl文件中的wsdlsoap:address的location无关,当然在这里它 们俩个是一致的),就可以看到发布的服务的wsdl描述,应该和前面的helloworld.wsdl的内容一样。

下面我们写一个groovy的客户端来测试一下这个web服务,代码大概象这样(更多的信息可以参考 http://docs.codehaus.org/display/GROOVY/Groovy+SOAP ,要运行下面的代码需要先安装一个groovy的用来除了soap的类库):

java 代码
 
  1. import groovy.net.soap.SoapClient  
  2. def proxy = new SoapClient("http://localhost:8085/HelloWorldService?wsdl")  
  3. println proxy.getGreetings("yanhua")  


运行它,在控制台可以看到 hello yanhua了吗?

有时候我们想查看一下客户端和服务器之间来往的soap消息,我们可以用AXIS提供的一个叫TCPMonitor的工具。先下载AXIS,解压后在AXIS的目录下运行如下的命令:java -classpath ./lib/axis.jar org.apache.axis.utils.tcpmon,这样会打开TCPMonitor这个工具。我们新建一个Listener,设置如下图:

</wsdlsoap:operation></wsdl:operation></wsdlsoap:binding></wsdl:binding></wsdl:operation></wsdl:porttype></wsdl:message></wsdl:message></schema></wsdl:types></wsdl:definitions>

我们把刚才的wsdl文件在groovy的运行目录下放置一份,并找到<wsdlsoap:address location="http://192.168.0.100:8085/HelloWorldService">把它的端口号改为8081,也就是<wsdlsoap:address location="http://192.168.0.100:8081/HelloWorldService">。再把上面的groovy代码改一下:

java 代码
 
  1. import groovy.net.soap.SoapClient  
  2. def proxy = new SoapClient("file:/E:/temp/groovy/helloworld.wsdl")  
  3. println proxy.getGreetings("yanhua")  


运行它,在TCPMonitor中就可以看到往返的soap消息了,对这个例子来说分别是:
发送到服务器的soap消息:
xml 代码
 
  1. POST /HelloWorldService HTTP/1.1  
  2. SOAPAction: ""  
  3. Content-Type: text/xml; charset=UTF-8  
  4. User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; XFire Client +http://xfire.codehaus.org)  
  5. Host: 127.0.0.1:8081  
  6. Expect: 100-continue  
  7. Content-Length: 308  
  8.   
  9. <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soap:Body><getGreetings xmlns="http://helloworld"><name xmlns="http://helloworld">yanhua</name></getGreetings></soap:Body></soap:Envelope>  
<soap:envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:body><getgreetings xmlns="http://helloworld"><name xmlns="http://helloworld"></getgreetings></soap:body></soap:envelope>

返回给客户端的soap消息:
xml 代码
 
  1. POST /HelloWorldService HTTP/1.1  
  2. SOAPAction: ""  
  3. Content-Type: text/xml; charset=UTF-8  
  4. User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; XFire Client +http://xfire.codehaus.org)  
  5. Host: 127.0.0.1:8081  
  6. Expect: 100-continue  
  7. Content-Length: 308  
  8.   
  9. <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soap:Body><getGreetings xmlns="http://helloworld"><name xmlns="http://helloworld">yanhua</name></getGreetings></soap:Body></soap:Envelope>  
<soap:envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:body><getgreetings xmlns="http://helloworld"><name xmlns="http://helloworld"></getgreetings></soap:body></soap:envelope>

</wsdlsoap:address></wsdlsoap:address>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值