用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

相关文章

JavaScript中的高级调试方法全攻略指南

《JavaScript中的高级调试方法全攻略指南》什么是高级JavaScript调试技巧,它比console.log有何优势,如何使用断点调试定位问题,通过本文,我们将深入解答这些问题,带您从理论到实... 目录观点与案例结合观点1观点2观点3观点4观点5高级调试技巧详解实战案例断点调试:定位变量错误性能分

修复已被利用的高危漏洞! macOS Sequoia 15.6.1发布

《修复已被利用的高危漏洞!macOSSequoia15.6.1发布》苹果公司于今日发布了macOSSequoia15.6.1更新,这是去年9月推出的macOSSequoia操作... MACOS Sequoia 15.6.1 正式发布!此次更新修复了一个已被黑客利用的严重安全漏洞,并解决了部分中文用户反馈的

Web服务器-Nginx-高并发问题

《Web服务器-Nginx-高并发问题》Nginx通过事件驱动、I/O多路复用和异步非阻塞技术高效处理高并发,结合动静分离和限流策略,提升性能与稳定性... 目录前言一、架构1. 原生多进程架构2. 事件驱动模型3. IO多路复用4. 异步非阻塞 I/O5. Nginx高并发配置实战二、动静分离1. 职责2

SpringBoot通过main方法启动web项目实践

《SpringBoot通过main方法启动web项目实践》SpringBoot通过SpringApplication.run()启动Web项目,自动推断应用类型,加载初始化器与监听器,配置Spring... 目录1. 启动入口:SpringApplication.run()2. SpringApplicat

解决Nginx启动报错Job for nginx.service failed because the control process exited with error code问题

《解决Nginx启动报错Jobfornginx.servicefailedbecausethecontrolprocessexitedwitherrorcode问题》Nginx启... 目录一、报错如下二、解决原因三、解决方式总结一、报错如下Job for nginx.service failed bec

Go语言网络故障诊断与调试技巧

《Go语言网络故障诊断与调试技巧》在分布式系统和微服务架构的浪潮中,网络编程成为系统性能和可靠性的核心支柱,从高并发的API服务到实时通信应用,网络的稳定性直接影响用户体验,本文面向熟悉Go基本语法和... 目录1. 引言2. Go 语言网络编程的优势与特色2.1 简洁高效的标准库2.2 强大的并发模型2.

Python Web框架Flask、Streamlit、FastAPI示例详解

《PythonWeb框架Flask、Streamlit、FastAPI示例详解》本文对比分析了Flask、Streamlit和FastAPI三大PythonWeb框架:Flask轻量灵活适合传统应用... 目录概述Flask详解Flask简介安装和基础配置核心概念路由和视图模板系统数据库集成实际示例Stre

在IntelliJ IDEA中高效运行与调试Spring Boot项目的实战步骤

《在IntelliJIDEA中高效运行与调试SpringBoot项目的实战步骤》本章详解SpringBoot项目导入IntelliJIDEA的流程,教授运行与调试技巧,包括断点设置与变量查看,奠定... 目录引言:为良驹配上好鞍一、为何选择IntelliJ IDEA?二、实战:导入并运行你的第一个项目步骤1

如何使用Maven创建web目录结构

《如何使用Maven创建web目录结构》:本文主要介绍如何使用Maven创建web目录结构的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录创建web工程第一步第二步第三步第四步第五步第六步第七步总结创建web工程第一步js通过Maven骨架创pytho

Java Web实现类似Excel表格锁定功能实战教程

《JavaWeb实现类似Excel表格锁定功能实战教程》本文将详细介绍通过创建特定div元素并利用CSS布局和JavaScript事件监听来实现类似Excel的锁定行和列效果的方法,感兴趣的朋友跟随... 目录1. 模拟Excel表格锁定功能2. 创建3个div元素实现表格锁定2.1 div元素布局设计2.