2. 用Tuscany构建你的第一个web service
这篇导航文章将一步步地介绍如何使用Tuscany构建web service。第一部分将介绍如何将Tuscany运行环境添加到Eclipse开发环境,第二部分将介绍如何利用Tuscany简单地创建web service。
2.1. 安装最新的Tuscany的Eclipse插件
本节将介绍Tuscany的Eclipse插件安装方法,它能够使你在Package Explorer中运行SCA组合构件。
从Eclipse菜单Help->Software Updates…->Find and Install,选择“Search for new features to install”,然后按下一步,在此步中点击“new Remote site…”按钮,输入name为“Tuscany”,站点URL为http://archive.apache.org/dist/tuscany/java/sca/1.4/tuscany-sca-1.4-updatesite/,添加完后选择刚才新增的远程站点,右击鼠标在弹出菜单中选择“Install”,在接下来的界面点击接收协议,按下一步即可完成安装。
2.2. 创建你的服务业务逻辑
2.2.1. 创建一个Java Project
在这一小节中你将创建一Eclipse的Java Project,用于构建组合构件应用。点击“New Java Project”按钮,加载新建Java Project对话框。输入Project Name为“ws”,Project Layout选择“Create separate folders for sources and class files.”
图-5
按提示到下一步,在Libraries选项卡中点击“Add Library...”按钮,把Tuscany Library添加到此Project中。
图-6
接着按提示完成新建ws工程。
图-7
2.2.2. 构建web service
首先新建个helloworld包
图-8
下一步新建个helloworld服务接口,接口内容如下
package helloworld; import org.osoa.sca.annotations.Remotable; @Remotable public interface HelloWorld { String sayHello(String name); } |
然后创建此接口的实现
package helloworld; public class HelloWorldImpl implements HelloWorld { public String sayHello(String name) { return "Hello " + name; } } |
2.2.3. 装配服务
现在服务接口跟实现都已经有了,接下来把它装配起来,提供组合服务。装配的信息都包含在.composite文件中。在src目录下新建helloworld.composite文件,其内容如下
<?xml version="1.0" encoding="UTF-8"?> <composite xmlns="http://www.osoa.org/xmlns/sca/1.0" xmlns:t="http://tuscany.apache.org/xmlns/sca/1.0" xmlns:c="http://helloworld" targetNamespace="http://helloworld" name="helloworld">
<component name="HelloWorldComponent"> <implementation.java class="helloworld.HelloWorldImpl"/> <service name="HelloWorld"> <binding.ws uri="http://localhost:8080/HelloWorld"/> </service> </component> </composite> |
此时ws工程内容如图-9
图-9
服务已经装配好,接下来我们来运行它。
2.3. 运行服务
在这一节中你将加载,刚刚装配的helloworld web service服务。在helloworld.composite文件上右击鼠标,选择“Run as->Tuscany”。之后Tuscany运行环境将启动,helloworld组合构件被加入它的域中,然后发布helloworld web service服务。
Eclipse控制台消息如图-10
图-10
你可以在浏览器中,输入http://localhost:8080/HelloWorld?wsdl,可以看到此web service的wsdl内容。
<?xml version="1.0" encoding="UTF-8"?> <wsdl:definitions targetNamespace="http://helloworld" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:ns0="http://helloworld" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"> <wsdl:types> <xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://helloworld" xmlns:ns="http://helloworld"> <xs:element name="sayHello"> <xs:complexType> <xs:sequence> <xs:element minOccurs="0" name="param0" nillable="true" type="xs:string"/> </xs:sequence> </xs:complexType>
</xs:element> <xs:element name="sayHelloResponse"> <xs:complexType> <xs:sequence> <xs:element minOccurs="0" name="return" nillable="true" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
</wsdl:types> <wsdl:message name="sayHelloRequest"> <wsdl:part name="parameters" element="ns0:sayHello"> </wsdl:part> </wsdl:message> <wsdl:message name="sayHelloResponse"> <wsdl:part name="parameters" element="ns0:sayHelloResponse"> </wsdl:part> </wsdl:message>
<wsdl:portType name="HelloWorldPortType"> <wsdl:operation name="sayHello"> <wsdl:input message="ns0:sayHelloRequest" wsaw:Action="urn:sayHello"> </wsdl:input> <wsdl:output message="ns0:sayHelloResponse" wsaw:Action="urn:sayHelloResponse"> </wsdl:output> </wsdl:operation> </wsdl:portType> <wsdl:binding name="HelloWorldHttpBinding" type="ns0:HelloWorldPortType">
<http:binding verb="POST"/> <wsdl:operation name="sayHello"> <http:operation location="HelloWorld/sayHello"/> <wsdl:input> <mime:content part="sayHello" type="text/xml"/> </wsdl:input> <wsdl:output> <mime:content part="sayHello" type="text/xml"/> </wsdl:output>
</wsdl:operation> </wsdl:binding> <wsdl:binding name="HelloWorldSOAP11Binding" type="ns0:HelloWorldPortType"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="sayHello"> <soap:operation soapAction="urn:sayHello" style="document"/> <wsdl:input> <soap:body use="literal"/> </wsdl:input>
<wsdl:output> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="HelloWorld"> <wsdl:port name="HelloWorldSOAP11port_http" binding="ns0:HelloWorldSOAP11Binding"> <soap:address location="http://192.168.209.1:8080/HelloWorld"/> </wsdl:port>
<wsdl:port name="HelloWorldHttpport" binding="ns0:HelloWorldHttpBinding"> <http:address location="http://192.168.209.1:8080/HelloWorld"/> </wsdl:port> </wsdl:service> </wsdl:definitions>
|
这样你就已经用Tuscany创建了web service。