在这一节,你将了解到如何将一个普通的 java 文件转换为一个 web 服务, services.xml 文件是如何定义的,如何发布这个 web 服务、如何获得这个服务的 WSDL 、如何创建 Client 代码、如何测试这个 web 服务。
首先打开 Eclipse ,创建一个普通的 java 工程,将 xfire 所需的 jar 和 xfire 的 jar 加入到工程所需的类库引用中。创建一个简单的 java 文件,这个 java 文件简单到只提供一个 add 方法。
| package com.kuaff.xfire.samples; public class MathService { public long add( int p1, int p2) { return p1 + p2; } } |
在 src 目录下创建 META-INF/xfire 目录,然后在 META-INF/xfire 目录下创建 services.xml 文件,文件内容为:
| <beans xmlns="http://xfire.codehaus.org/config/1.0"> <service> <name>MathService</name> <namespace>http://www.kuaff.com/xfire/samples/MathService</namespace> <serviceClass>com.kuaff.xfire.samples.MathService</serviceClass> </service> </beans>
|
这个文档定义了你要发布的 web 服务,这个定义了一个名为 MathService 的服务,服务类为 com.kuaff.xfire.samples.MathService 。
这样我们的一个简单的 web 服务就开发完成了,下面就要把它发布出去。
将工程 bin 目录下的所有的文件复制到第一节中配置的 tomcat/webapps/xfire/WEB-INF/classes 文件夹下,启动 tomcat ,你就可以检查这个 web 服务是否发布成功了。
打开浏览器,在浏览器地址栏中输入 http://localhost:8080/xfire/services/ ,正常情况下应该浏览器应该显示类似下图所示的页面。
<!-- [if gte vml 1]><v:shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"> <v:stroke joinstyle="miter" /> <v:formulas> <v:f eqn="if lineDrawn pixelLineWidth 0" /> <v:f eqn="sum @0 1 0" /> <v:f eqn="sum 0 0 @1" /> <v:f eqn="prod @2 1 2" /> <v:f eqn="prod @3 21600 pixelWidth" /> <v:f eqn="prod @3 21600 pixelHeight" /> <v:f eqn="sum @0 0 1" /> <v:f eqn="prod @6 1 2" /> <v:f eqn="prod @7 21600 pixelWidth" /> <v:f eqn="sum @8 21600 0" /> <v:f eqn="prod @7 21600 pixelHeight" /> <v:f eqn="sum @10 21600 0" /> </v:formulas> <v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect" /> <o:lock v:ext="edit" aspectratio="t" /> </v:shapetype><v:shape id="_x0000_i1025" type="#_x0000_t75" style='width:195pt; height:90.75pt;mso-wrap-distance-left:9.05pt;mso-wrap-distance-right:9.05pt; mso-position-horizontal-relative:page;mso-position-vertical-relative:page'> <v:imagedata src="file:///C:/DOCUME~1/SMALLN~1/LOCALS~1/Temp/msohtml1/01/clip_image001.png" o:title="" embosscolor="#000002" /> <o:lock v:ext="edit" aspectratio="f" /> </v:shape><![endif]--><!-- [if !vml]--><!-- [endif]-->
<!-- [if gte vml 1]><v:shape id="_x0000_s1026" type="#_x0000_t75" style='position:absolute;left:0; text-align:left;margin-left:27pt;margin-top:7.8pt;width:42pt;height:31.5pt; z-index:-1' wrapcoords="11974 318 7513 5400 6809 7941 6574 9529 5400 13976 5870 15565 0 16835 0 18424 10800 20647 6574 20647 6574 20965 15730 20965 21365 19694 21600 16200 18783 15565 19957 10482 19487 4765 16200 1271 14557 318 11974 318" o:allowoverlap="f"> <v:imagedata src="file:///C:/DOCUME~1/SMALLN~1/LOCALS~1/Temp/msohtml1/01/clip_image003.wmz" o:title="" /> <w:wrap type="tight" /> </v:shape><![endif]--><!-- [if !vml]--><!-- [endif]-->
注意:请在浏览器中输入 http://localhost:8080/xfire/services/ 而不是 http://localhost:8080/xfire/services ,虽然两者显示的页面相同,但是点击页面上的链接,后者的链接会出错,因为后者后面少加了一个“ / ”。
点击 [wsdl] 链接,可以查看这个 web 服务的 wsdl 文档。
<!-- [if gte vml 1]><v:shape id="_x0000_i1026" type="#_x0000_t75" style='width:287.25pt;height:199.5pt;mso-wrap-distance-left:9.05pt; mso-wrap-distance-right:9.05pt;mso-position-horizontal-relative:page; mso-position-vertical-relative:page'> <v:imagedata src="file:///C:/DOCUME~1/SMALLN~1/LOCALS~1/Temp/msohtml1/01/clip_image005.png" o:title="" embosscolor="#000002" /> <o:lock v:ext="edit" aspectratio="f" /> </v:shape><![endif]--><!-- [if !vml]--><!-- [endif]-->
如果在你的机器上的显示如上面所示,则说明你的这个 web 服务发布成功,可以正常提供基于 http 的 web 服务。
下面一个问题就是如何开发一个 Client ,来消费(使用)这个 web 服务。
XFire 、 Axis 、 asp.net 以及其它的一些商业产品都提供了根据 wsdl 文档创建客户端代码的工具。这里采用 Xfire 提供的 wsgen 工具来创建客户端的访问代码。
Wsgen 是 xfire 提供的一个 ant task , task 的申明如下:
| <taskdef name= "wsgen" classname= "org.codehaus.xfire.gen.WsGenTask" classpathref= "xfire 的 jar 路径 " /> |
这样,就可以在 build.xml 文件中使用这个 task :
| <wsgen outputDirectory= "client" wsdl= "MathService.wsdl" package= "com.kuaff.xfire.samples" /> |
outputDirectory 属性定义创建的代码所在的文件夹, wsdl 是 web 服务的 wsdl 文件, package 代表创建的代码的 packege 。还可以通过 binding 属性制定 bind 类型: jaxb 或者是 xmlbeans ,
这样,就会在 client 文件夹下创建多个 java 文件,这些文件提供了访问 web 服务的方法。
在这里声明一点,创建的代码大量的使用了 java 注释,这个 JSE5 中提供的新特性,所以你需要使用 JSE5 编译, Eclipse 也必须要 3.1 以上的版本。
这段 ant 脚本将创建三个文件: MathServicePortType 、 MathServiceImpl 和 MathServiceClient 。 MathServicePortType 是这个 web 服务的客户端接口存根、 MathServiceImpl 实现了这个接口。 MathServiceClient 封装了访问这个 web 服务的方法。
最后,你可以创建一个单元测试类,用来测试这个客户端类。这个类的内容如下:
package com.kuaff.xfire.samples;
import junit.framework.TestCase;
public class MathServiceClientTest extends TestCase
{
public void testAdd()
{
MathServiceClient client = new MathServiceClient();
MathServicePortType ms = client.getMathServiceHttpPort();
long result = ms.add(10, 20);
assertEquals (result, 30);
}
}
277

被折叠的 条评论
为什么被折叠?



