目录:
[一]、概述
以?XFire webservice入门开发实例?中发布的服务端为例,WSDL:http://demo.micmiu.com:8080/xfire-base-demo/services/HelloWorldService?。
创建一个客户端调用的工程:
class="brush: bash; gutter: true; first-line: 1">mvn archetype:create -DgroupId=com.micmiu.xfire.demo -DartifactId=xfire-client-demo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
[二]、根据默认配置实现代理客户端
首先:编写一个像之前文章中服务端一样的服务接口类:HelloWorldService.java
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
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
运行日志:
----> 初始客户端 <---- Hi,Michael welcome,see more http://www.micmiu.com ----> 客户端调用结束 <----
[三]、根据JSR 181注解实现代理客户端
修改上面 [一] 中的服务接口类,添加@webService?注解:HelloWorldService.java
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
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
运行日志:
----> 初始客户端 <---- Hi,Michael welcome,see more http://www.micmiu.com ----> 客户端调用结束 <----
[四]、根据WSDL自动生成客户端代码
目录结构如下:
E:\software code\xFire>tree ├─client-src │ └─lib
运用WsGen命令生成代码:
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
参数说明:
运行日志:
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.
客户端代码生成后的目录结构如下:
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
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(" ----> 客户端调用结束 <---- "); } }
运行结果:
----> 初始客户端 <---- Hi,Michael welcome,see more http://www.micmiu.com ----> 客户端调用结束 <----
[五]、动态客户端的实现
编写测试代码:HelloClient4Dynamic.java
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
运行结果:
----> 初始客户端 <---- Hi,Michael welcome,see more http://www.micmiu.com ----> 客户端调用结束 <----