(1) eclipse 默认的源文件夹是:WEB-INF/src 输出文件夹是:WEB-INF/classes (2)拷贝 下载的xfire 中的*.jar 到新建的xfireDemo 的workspace内 的WEB-INF/lib 内包括:xfire-all-1.2.6.jar和xfire-1.2.6/lib 内的所有的 jar包
(3) 在eclipse内设置Java Build Path 导入需要的 jar包如下图:
(4)在WEB-INF/src内新建一个类: package com.ywb.xfire; public class HelloService {
public String sayHello(String name) { return name + \"你好!\"; } }
(5)在WEB-INF/src内新建一META-INF/xfire/services.xml文件 ,该文件用于声明一个service。service.xml文件的内容如下:
(6)在WEB-INF内新建一个web.xml,内容如下: PUBLIC \"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN\" \"http://java.sun.com/dtd/web-app_2_3.dtd\"> org.codehaus.xfire.transport.http.XFireConfigurableServlet
(7)启动tomcat;正确启动tomcat之后,在IE地址栏里输入:
http://localhost:8080/xfireDemo/services/将会出现如下的页面:该页面正常显示了刚才部署的HelloService。注意(有的浏览器不能打开,最好用IE浏
览器)如下图:
如果能正确显示上图,说明刚才的部署成功了 单击[wsdl],将会出现
http://localhost:8080/xfireDemo/services/HelloService?wsdl,即生成的一个wsdl文件。
project的结构是:如下图
(8)生成web service客户端调用文件
xfire提供了两种生成客户端测试的方式,一种提供了ant脚本,另一种是提供了xfire的eclipse插件;本文介绍了使用ant脚本的方式生成客户端的方式。首先在项目xfireDemo中增加一个build.xml文件。xfire提供了一个ant任务:
注意在:ant构建的过程中必须要保证tomcat在启动中 控制台信息:
Buildfile: D:\\eclipse\\workspace\\xfireDemo\\build.xml genfiles:
[wsgen] 2007-11-5 13:20:53 org.codehaus.xfire.gen.Wsdl11Generator generate
[wsgen] 信息: Generating code for WSDL at
http://localhost:8080/xfire/services/HelloService?wsdl with a base URI of http://localhost:8080/xfire/services/HelloService?wsdl [wsgen] 2007-11-5 13:20:55
org.codehaus.xfire.gen.jsr181.AbstractServiceGenerator generate [wsgen] 信息: Creating class
com.ywb.xfire.client.HelloServicePortType [wsgen] 2007-11-5 13:20:55
org.codehaus.xfire.gen.jsr181.AbstractServiceGenerator generate [wsgen] 信息: Creating class com.ywb.xfire.client.HelloServiceImpl [wsgen] com\\ywb\\xfire\\client\\HelloServiceClient.java [wsgen] com\\ywb\\xfire\\client\\HelloServiceImpl.java [wsgen] com\\ywb\\xfire\\client\\HelloServicePortType.java BUILD SUCCESSFUL Total time: 13 seconds
build成功,刷新你的工程xfireDemo 就会看到 多出了一个文件夹client里面的内容是:HelloServiceClient.java、HelloServiceImpl.java、HelloServicePortType.java
(9)在xfireDemo 内新建2个文件夹分别命名为:classes和test
目的:test文件夹内放置测试文件,classes内放置 client和test的类文件 在次编辑 Java Build Path 如下图进行设置:
在test内新建测试类 package com.ywb.test;
import com.ywb.xfire.client.HelloServiceClient; import com.ywb.xfire.client.HelloServicePortType; public class TestClient { /**
* @param args
*/
public static void main(String[] args) { // TODO Auto-generated method stub System.out.println(testClient(\"ywb\")); }
public static String testClient(String name){
HelloServiceClient helloSC=new HelloServiceClient(); HelloServicePortType
helloSP=helloSC.getHelloServiceHttpPort(); String result=helloSP.sayHello(name); return result; } }
整个工程的结构是如下图:
运行TestClient.java 就会得到: ywb你好! 到此完工。 初次学习!!!
因篇幅问题不能全部显示,请点此查看更多更全内容