XFire WebService开发快速起步
环境:
xfire-1.2.6
jdk1.5以上
myeclipse8.5
Tomcat6.0
windows
以下是开发步骤:
1、打开MyEclipse,新建一个WebServices工程,如下图所示
然后一路next,直到完成。
服务端开发:
WebServices工程建立完成后,新建一个WebServices,点击New Web Services按钮,如下图所示:
点击Next按钮
Web Serivces Name 中填入你的webservices服务名
点击Finish按钮,在本例中MyEclipse会自动在com.testxfire.services中生成两个类,一个是接口类,一个实现类。
package com.testxfire.services; public interface ISayHelloServices { public String sayHello(String name); }
package com.testxfire.services; public class SayHelloServicesImpl { public String sayHello(String name){ return "Hello,"+name; }; }
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet> <servlet-name>XFireServlet</servlet-name> <servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>XFireServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> </web-app>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://xfire.codehaus.org/config/1.0"> <service> <name>SayHelloServices</name> <serviceClass>com.testxfire.services.ISayHelloServices</serviceClass> <implementationClass>com.testxfire.services.SayHelloServicesImpl</implementationClass> <style>wrapped</style> <use>literal</use> <scope>application</scope> </service> </beans>
至此服务端完全开发完成。
我们在IE中输入URL:Http://localhost:8080/XfireDemo/services/SayHelloServices?wsdl,如果出现下面图片则说明WebServices的服务端开发成功。
下面是客户端的代码:
package com.testxfire.client; import com.testxfire.services.ISayHelloServices; import java.net.MalformedURLException; import org.codehaus.xfire.XFireFactory; 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; public class SayHelloClient(){ public static void main(String[] args){ String url = "Http://localhost:8080/XfireDemo/services/SayHelloServices"; Service srvModel = new ObjectServiceFactory().create(ISayHelloServices.class); XFireProxyFactory factory = new XFireProxyFactory(XFireFactory.newInstance().getXFire()); Services srvc = (Services)factory.create(srvModel, url); System.out.println(srvc.sayHello('bill')); } }