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

2024-04-21 14:38

本文主要是介绍用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>

这篇关于用Tuscany、Axis、groovy来发布和调试Web Service的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/923321

相关文章

利用Python调试串口的示例代码

《利用Python调试串口的示例代码》在嵌入式开发、物联网设备调试过程中,串口通信是最基础的调试手段本文将带你用Python+ttkbootstrap打造一款高颜值、多功能的串口调试助手,需要的可以了... 目录概述:为什么需要专业的串口调试工具项目架构设计1.1 技术栈选型1.2 关键类说明1.3 线程模

使用Python构建一个Hexo博客发布工具

《使用Python构建一个Hexo博客发布工具》虽然Hexo的命令行工具非常强大,但对于日常的博客撰写和发布过程,我总觉得缺少一个直观的图形界面来简化操作,下面我们就来看看如何使用Python构建一个... 目录引言Hexo博客系统简介设计需求技术选择代码实现主框架界面设计核心功能实现1. 发布文章2. 加

售价599元起! 华为路由器X1/Pro发布 配置与区别一览

《售价599元起!华为路由器X1/Pro发布配置与区别一览》华为路由器X1/Pro发布,有朋友留言问华为路由X1和X1Pro怎么选择,关于这个问题,本期图文将对这二款路由器做了期参数对比,大家看... 华为路由 X1 系列已经正式发布并开启预售,将在 4 月 25 日 10:08 正式开售,两款产品分别为华

利用Python快速搭建Markdown笔记发布系统

《利用Python快速搭建Markdown笔记发布系统》这篇文章主要为大家详细介绍了使用Python生态的成熟工具,在30分钟内搭建一个支持Markdown渲染、分类标签、全文搜索的私有化知识发布系统... 目录引言:为什么要自建知识博客一、技术选型:极简主义开发栈二、系统架构设计三、核心代码实现(分步解析

使用Python自建轻量级的HTTP调试工具

《使用Python自建轻量级的HTTP调试工具》这篇文章主要为大家详细介绍了如何使用Python自建一个轻量级的HTTP调试工具,文中的示例代码讲解详细,感兴趣的小伙伴可以参考一下... 目录一、为什么需要自建工具二、核心功能设计三、技术选型四、分步实现五、进阶优化技巧六、使用示例七、性能对比八、扩展方向建

微信公众号脚本-获取热搜自动新建草稿并发布文章

《微信公众号脚本-获取热搜自动新建草稿并发布文章》本来想写一个自动化发布微信公众号的小绿书的脚本,但是微信公众号官网没有小绿书的接口,那就写一个获取热搜微信普通文章的脚本吧,:本文主要介绍微信公众... 目录介绍思路前期准备环境要求获取接口token获取热搜获取热搜数据下载热搜图片给图片加上标题文字上传图片

JSON Web Token在登陆中的使用过程

《JSONWebToken在登陆中的使用过程》:本文主要介绍JSONWebToken在登陆中的使用过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录JWT 介绍微服务架构中的 JWT 使用结合微服务网关的 JWT 验证1. 用户登录,生成 JWT2. 自定义过滤

SpringKafka消息发布之KafkaTemplate与事务支持功能

《SpringKafka消息发布之KafkaTemplate与事务支持功能》通过本文介绍的基本用法、序列化选项、事务支持、错误处理和性能优化技术,开发者可以构建高效可靠的Kafka消息发布系统,事务支... 目录引言一、KafkaTemplate基础二、消息序列化三、事务支持机制四、错误处理与重试五、性能优

一文教你如何将maven项目转成web项目

《一文教你如何将maven项目转成web项目》在软件开发过程中,有时我们需要将一个普通的Maven项目转换为Web项目,以便能够部署到Web容器中运行,本文将详细介绍如何通过简单的步骤完成这一转换过程... 目录准备工作步骤一:修改​​pom.XML​​1.1 添加​​packaging​​标签1.2 添加

新特性抢先看! Ubuntu 25.04 Beta 发布:Linux 6.14 内核

《新特性抢先看!Ubuntu25.04Beta发布:Linux6.14内核》Canonical公司近日发布了Ubuntu25.04Beta版,这一版本被赋予了一个活泼的代号——“Plu... Canonical 昨日(3 月 27 日)放出了 Beta 版 Ubuntu 25.04 系统镜像,代号“Pluc