目录:
- 概述
- 根据默认配置实现代理客户端
- 根据JSR 181注解实现代理客户端
- 根据WSDL自动生成客户端代码
- 动态客户端的实现
[一]、概述
以 XFire webservice入门开发实例 中发布的服务端为例,WSDL:http://demo.micmiu.com:8080/xfire-base-demo/services/HelloWorldService 。
创建一个客户端调用的工程:
1 |
mvn archetype:create -DgroupId=com.micmiu.xfire.demo -DartifactId=xfire-client-demo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false |
[二]、根据默认配置实现代理客户端
首先:编写一个像之前文章中服务端一样的服务接口类:HelloWorldService.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.micmiu.xfire.demo.client; import javax.jws.WebService; /** * * @blog http://www.micmiu.com * @author Michael */ public interface HelloWorldService { /** * return welcome * @param username * @return */ String sayHello(String username); } |
客户端调用测试类:HelloClient4Default.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
package com.micmiu.xfire.demo.client; import org.codehaus.xfire.client.XFireProxyFactory; import org.codehaus.xfire.service.Service; import org.codehaus.xfire.service.binding.ObjectServiceFactory; /** * * @blog http://www.micmiu.com * @author Michael */ public class HelloClient4Default { /** * @param args */ public static void main(String[] args) { String wsURL = "http://demo.micmiu.com:8080/xfire-base-demo/services/HelloWorldService"; try { System.out.println(" ----> 初始客户端 <---- "); Service serviceModel = new ObjectServiceFactory() .create(HelloWorldService.class); // Service serviceModel = new ObjectServiceFactory().create( // HelloWorldService.class, "HelloWorldService", // "http://demo.xfire.micmiu.com/HelloWoldService", null); HelloWorldService client = (HelloWorldService) new XFireProxyFactory() .create(serviceModel, wsURL); System.out.println(client.sayHello("Michael")); System.out.println(" ----> 客户端调用结束 <---- "); } catch (Exception e) { e.printStackTrace(); } } } |
ps:注意代码中的 webservice的URL 没有后缀 ?wsdl
运行日志:
1 2 3 |
----> 初始客户端 <---- Hi,Michael welcome,see more http://www.micmiu.com ----> 客户端调用结束 <---- |
[三]、根据JSR 181注解实现代理客户端
修改上面 [一] 中的服务接口类,添加@webService 注解:HelloWorldService.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.micmiu.xfire.demo.client; import javax.jws.WebService; /** * * @blog http://www.micmiu.com * @author Michael */ @WebService public interface HelloWorldService { /** * return welcome * @param username * @return */ String sayHello(String username); } |
编写客户端代码:HelloClient4JSR181Annotation.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package com.micmiu.xfire.demo.client; import org.codehaus.xfire.annotations.AnnotationServiceFactory; import org.codehaus.xfire.client.XFireProxyFactory; import org.codehaus.xfire.service.Service; /** * * @blog http://www.micmiu.com * @author Michael */ public class HelloClient4JSR181Annotation { /** * @param args */ public static void main(String[] args) { String wsURL = "http://demo.micmiu.com:8080/xfire-base-demo/services/HelloWorldService"; try { System.out.println(" ----> 初始客户端 <---- "); Service serviceModel = new AnnotationServiceFactory() .create(HelloWorldService.class); HelloWorldService client = (HelloWorldService) new XFireProxyFactory() .create(serviceModel, wsURL); System.out.println(client.sayHello("Michael")); System.out.println(" ----> 客户端调用结束 <---- "); } catch (Exception e) { e.printStackTrace(); } } } |
ps:注意代码中的 webservice的URL 没有后缀 ?wsdl
运行日志:
1 2 3 |
----> 初始客户端 <---- Hi,Michael welcome,see more http://www.micmiu.com ----> 客户端调用结束 <---- |
[四]、根据WSDL自动生成客户端代码
目录结构如下:
1 2 3 4 |
E:\software code\xFire>tree ├─client-src │ └─lib |
运用WsGen命令生成代码:
1 |
java -cp .;./lib/* org.codehaus.xfire.gen.WsGen -wsdl http://demo.micmiu.com:8080/xfire-base-demo/services/HelloWorldService?wsdl -o ./client-src -p com.micmiu.xfire.demo.base.client -overwrite true |
参数说明:
- -wsdl : 指定wsdl文件或者URL
- -o : 指定输出目录
- -package : 指定客户端代码的包结构
- -binding : 指定绑定:jaxb 或 xmlbeans
- -overwrite : 指定是否覆盖
运行日志:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
E:\software code\xFire>java -cp .;./lib/* org.codehaus.xfire.gen.WsGen -wsdl htt p://demo.micmiu.com:8080/xfire-base-demo/services/HelloWorldService?wsdl -o ./cl ient-src -p com.micmiu.xfire.demo.base.client -overwrite true Running WsGen... wsdl : http://demo.micmiu.com:8080/xfire-base-demo/services/HelloWorldService ?wsdl package : com.micmiu.xfire.demo.base.client output : ./client-src binding : externalBindings : baseURI : profile : explictAnnotation : false overwrite : true serverStub : true forceBare : false 2012-7-30 18:26:22 org.codehaus.xfire.gen.Wsdl11Generator generate 信息: Generating code for WSDL at http://demo.micmiu.com:8080/xfire-base-demo/se rvices/HelloWorldService?wsdl with a base URI of http://demo.micmiu.com:8080/xfi re-base-demo/services/HelloWorldService?wsdl 2012-7-30 18:26:23 org.codehaus.xfire.gen.jsr181.AbstractServiceGenerator genera te 信息: Creating class com.micmiu.xfire.demo.base.client.HelloWorldServicePortType 2012-7-30 18:26:23 org.codehaus.xfire.gen.jsr181.AbstractServiceGenerator genera te 信息: Creating class com.micmiu.xfire.demo.base.client.HelloWorldServiceImpl com\micmiu\xfire\demo\hellowoldservice\ObjectFactory.java com\micmiu\xfire\demo\hellowoldservice\SayHello.java com\micmiu\xfire\demo\hellowoldservice\SayHelloResponse.java com\micmiu\xfire\demo\hellowoldservice\package-info.java com\micmiu\xfire\demo\base\client\HelloWorldServiceClient.java com\micmiu\xfire\demo\base\client\HelloWorldServiceImpl.java com\micmiu\xfire\demo\base\client\HelloWorldServicePortType.java Done. |
客户端代码生成后的目录结构如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
E:\software code\xFire>tree /F client-src 卷 tools 的文件夹 PATH 列表 卷序列号为 AA19-63C1 E:\SOFTWARE CODE\XFIRE\CLIENT-SRC ├─com │ └─micmiu │ └─xfire │ └─demo │ ├─base │ │ └─client │ │ HelloWorldServiceClient.java │ │ HelloWorldServiceImpl.java │ │ HelloWorldServicePortType.java │ │ │ └─hellowoldservice │ ObjectFactory.java │ package-info.java │ SayHello.java │ SayHelloResponse.java │ └─META-INF └─xfire services.xml |
把上面的源代码copy到项目中即可,编写测试代码:HelloWorldGenClientTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package com.micmiu.xfire.demo.base.client; /** * * @blog http://www.micmiu.com * @author Michael */ public class HelloWorldGenClientTest { public static void main(String[] args) throws Exception { System.out.println(" ----> 初始客户端 <---- "); HelloWorldServiceClient service = new HelloWorldServiceClient(); HelloWorldServicePortType client = service .getHelloWorldServiceHttpPort(); System.out.println(client.sayHello("Michael")); System.out.println(" ----> 客户端调用结束 <---- "); } } |
运行结果:
1 2 3 |
----> 初始客户端 <---- Hi,Michael welcome,see more http://www.micmiu.com ----> 客户端调用结束 <---- |
[五]、动态客户端的实现
编写测试代码:HelloClient4Dynamic.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
package com.micmiu.xfire.demo.client; import java.net.URL; import org.codehaus.xfire.client.Client; /** * * @blog http://www.micmiu.com * @author Michael */ public class HelloClient4Dynamic { /** * @param args */ public static void main(String[] args) { String wsURL = "http://demo.micmiu.com:8080/xfire-base-demo/services/HelloWorldService?wsdl"; try { System.out.println(" ----> 初始客户端 <---- "); Client client = new Client(new URL(wsURL)); Object[] results = client.invoke("sayHello", new Object[] { "Michael" }); System.out.println(results[0]); System.out.println(" ----> 客户端调用结束 <---- "); } catch (Exception e) { e.printStackTrace(); } } } |
ps:注意代码中的 webservice的URL 有后缀 ?wsdl
运行结果:
1 2 3 |
----> 初始客户端 <---- Hi,Michael welcome,see more http://www.micmiu.com ----> 客户端调用结束 <---- |
—
原创文章,转载请注明: 转载自micmiu – 软件开发+生活点滴[ http://www.micmiu.com/ ]
本文链接地址: http://www.micmiu.com/soa/webservice/xfire-clients-demo/
❓ 您好,我使用xfire动态调用webservice的时候参数传过去总是为空。能正确调用和正确返回。就是取不到参数。请教一下