一个基础的XFire WebService快速开发
环境:XFire-1.2.6JDK1.5MyEclipse 6.5Tomcat-5.5.27Windows XP Professional简体中文版?软件下载地址:[url]http://repository.codehaus.org/org/codehaus/xfire/xfire-distribution/1.2.6/xfire-distribution-1.2.6.zip[/url][url]http://apache.mirror.phpchina.com/tomcat/tomcat-5/v5.5.27/bin/apache-tomcat-5.5.27.zip[/url]?有关WebService的概念、原理、数据发现、描述、绑定等过程、方式也不说了。这里就只关注如何快速开发出来一个通用的、易懂的Hello World例子。?以下是开发步骤:?1、创建工程?打开MyEclipse 6.5,新建一个WebService工程。如下图??然后一路next,直到完成。?创建完成后,打开生成的web.xml文件,可以看到,XFire已经配置好了。
??2、创建WebService服务?创建est"> <wsdlsoap:body use="literal" /> </wsdl:input>- <wsdl:output name="exampleResponse"> <wsdlsoap:body use="literal" /> </wsdl:output> </wsdl:operation> </wsdl:binding>- <wsdl:service name="WebServiceXFire">- <wsdl:port name="WebServiceXFireHttpPort" binding="tns:WebServiceXFireHttpBinding"> <wsdlsoap:address location="http://localhost:8080/WebServiceXFire/services/WebServiceXFire" /> </wsdl:port> </wsdl:service> </wsdl:definitions>?假设你已经配置了Tomcat服务器,并完成了WebService服务端的部署。那么,现在就启动Tomcat,然后:???输入访问地址:[url]http://localhost:8080/xfire126Demo/services/MyService?wsdl[/url]? ,MyService是services.xml中name的值然后go一把!???这样,出现上上面的结果,表明测试成功了。?4、生成客户端代码?????很郁闷,这个生成的客户端代码一部分跑到服务端的包里面了。真是垃圾,rubbish!!!?但是,这就是MyEclipse的功能,我改变不了。?5、客户端测试?下面就耐心看怎么用这个客户端代码。打开生成的代码如下:
package wstest.client; import java.net.MalformedURLException; import java.util.Collection; import java.util.HashMap; import javax.xml.namespace.QName; import org.codehaus.xfire.XFireRuntimeException; import org.codehaus.xfire.aegis.AegisBindingProvider; import org.codehaus.xfire.annotations.AnnotationServiceFactory; import org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations; import org.codehaus.xfire.client.XFireProxyFactory; import org.codehaus.xfire.jaxb2.JaxbTypeRegistry; import org.codehaus.xfire.service.Endpoint; import org.codehaus.xfire.service.Service; import org.codehaus.xfire.soap.AbstractSoapBinding; import org.codehaus.xfire.transport.TransportManager; public class MyServiceClient { private static XFireProxyFactory proxyFactory = new XFireProxyFactory(); private HashMap endpoints = new HashMap(); private Service service0; public MyServiceClient() { create0(); Endpoint MyServicePortTypeLocalEndpointEP = service0 .addEndpoint(new QName("http://server.wstest", "MyServicePortTypeLocalEndpoint"), new QName("http://server.wstest", "MyServicePortTypeLocalBinding"), "xfire.local://MyService"); endpoints.put(new QName("http://server.wstest", "MyServicePortTypeLocalEndpoint"), MyServicePortTypeLocalEndpointEP); Endpoint MyServiceHttpPortEP = service0 .addEndpoint(new QName("http://server.wstest", "MyServiceHttpPort"), new QName("http://server.wstest", "MyServiceHttpBinding"), "http://localhost:8080/xfire126Demo/services/MyService"); endpoints.put(new QName("http://server.wstest", "MyServiceHttpPort"), MyServiceHttpPortEP); } public Object getEndpoint(Endpoint endpoint) { try { return proxyFactory.create((endpoint).getBinding(), (endpoint).getUrl()); } catch (MalformedURLException e) { throw new XFireRuntimeException("Invalid URL", e); } } public Object getEndpoint(QName name) { Endpoint endpoint = ((Endpoint) endpoints.get((name))); if ((endpoint) == null) { throw new IllegalStateException("No such endpoint!"); } return getEndpoint((endpoint)); } public Collection getEndpoints() { return endpoints.values(); } private void create0() { TransportManager tm = (org.codehaus.xfire.XFireFactory.newInstance().getXFire().getTransportManager()); HashMap props = new HashMap(); props.put("annotations.allow.interface", true); AnnotationServiceFactory asf = new AnnotationServiceFactory(new Jsr181WebAnnotations(), tm, new AegisBindingProvider(new JaxbTypeRegistry())); asf.setBindingCreationEnabled(false); service0 = asf.create((wstest.client.MyServicePortType.class), props); { AbstractSoapBinding soapBinding = asf.createSoap11Binding(service0, new QName("http://server.wstest", "MyServiceHttpBinding"), "http://schemas.xmlsoap.org/soap/http"); } { AbstractSoapBinding soapBinding = asf.createSoap11Binding(service0, new QName("http://server.wstest", "MyServicePortTypeLocalBinding"), "urn:xfire:transport:local"); } } public MyServicePortType getMyServicePortTypeLocalEndpoint() { return ((MyServicePortType)(this).getEndpoint(new QName("http://server.wstest", "MyServicePortTypeLocalEndpoint"))); } public MyServicePortType getMyServicePortTypeLocalEndpoint(String url) { MyServicePortType var = getMyServicePortTypeLocalEndpoint(); org.codehaus.xfire.client.Client.getInstance(var).setUrl(url); return var; } public MyServicePortType getMyServiceHttpPort() { return ((MyServicePortType)(this).getEndpoint(new QName("http://server.wstest", "MyServiceHttpPort"))); } public MyServicePortType getMyServiceHttpPort(String url) { MyServicePortType var = getMyServiceHttpPort(); org.codehaus.xfire.client.Client.getInstance(var).setUrl(url); return var; } public static void main(String[] args) { MyServiceClient client = new MyServiceClient(); //create a default service endpoint MyServicePortType service = client.getMyServiceHttpPort(); //TODO: Add custom client code here // //service.yourServiceOperationHere(); System.out.println("test client completed"); System.exit(0); } }??看得很晕,不知道啥意思,但是从“TODO”标记处,我知道了:????//TODO: Add custom client code here