1、在服务提供者告诉interface的情况下
2、通过WSDL创建一个动态的客户端
package com.exigen.www.client; import java.net.MalformedURLException; import java.net.URL; import org.codehaus.xfire.annotations.AnnotationServiceFactory; import org.codehaus.xfire.client.Client; import org.codehaus.xfire.client.XFireProxyFactory; import org.codehaus.xfire.service.Service; import org.codehaus.xfire.service.binding.ObjectServiceFactory; import com.exigen.www.HelloWorldService; public class HelloWorldClient { /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { dynamicClientTest(); knownInterfaceClientTest(); } /** * 服务提供者告诉你interface * @throws MalformedURLException */ public static void knownInterfaceClientTest() throws MalformedURLException{ Service serviceModel = new ObjectServiceFactory().create(HelloWorldService.class); HelloWorldService helloWorldService = (HelloWorldService)new XFireProxyFactory().create(serviceModel, "http://localhost:8080/XFire/services/HelloWorldService"); System.out.println(helloWorldService.sayHello()); System.out.println("============================"); Service serviceModel1 = new AnnotationServiceFactory().create(HelloWorldService.class); HelloWorldService client = (HelloWorldService) new XFireProxyFactory().create(serviceModel1, "http://localhost:8080/XFire/services/HelloWorldService"); System.out.println(client.sayHello()); } /** * 通过WSDL创建一个动态的客户端 * @throws MalformedURLException * @throws Exception */ public static void dynamicClientTest() throws MalformedURLException, Exception{ Client client = new Client(new URL("http://localhost:8080/XFire/services/HelloWorldService?wsdl")); Object[] results = client.invoke("sayHello", new Object[]{}); System.out.println(results[0]); System.out.println("============================"); } }
3、使用Ant工具通过WSDL生成客户端
<?xml version="1.0"?> <project name="wsgen" default="wsgen" basedir="."> <path id="classpathId"> <fileset dir="./WebRoot/WEB-INF/lib"> <include name="*.jar" /> </fileset> </path> <taskdef classpathref="classpathId" name="wsgen" classname="org.codehaus.xfire.gen.WsGenTask"> </taskdef> <target name="wsgen" description="generate client"> <wsgen outputDirectory="./src/" wsdl="abc.wsdl" binding="xmlbeans" package="com.abc.p" overwrite="true" /> </target> </project>
请注意,脚本中有一个参数binding,可以指定如下两种不同的方式:
- axb(Java Architecture for XML Binding,https://jaxb.dev.java.net/):使用此种方式时,会自动生成更多的Request和Resopnse类。
- xmlbeans,调用方式如下:
AbcServiceClient client = new AbcServiceClient(); String url = "http://localhost:8080/xfireTest/services/TestService"; String result = client.getAbcPort(url).sayHello("Robin");