同样,我们还是把Web服务端和客户端分成两个项目,来写
下面展示的是Web服务端的代码
首先是web.xml和services.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.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>XFireHandlerDemo</name> <namespace>http://www.jadyer.com/XFireHandlerDemo</namespace> <serviceClass>com.jadyer.server.HelloService</serviceClass> <implementationClass>com.jadyer.server.HelloServiceImpl</implementationClass> <!-- 请留意下面的设置 --> <inHandlers> <handler handlerClass="com.jadyer.server.handler.ServerInHandler"/> </inHandlers> <outHandlers> <handler handlerClass="com.jadyer.server.handler.ServerOutHandler"/> </outHandlers> </service> </beans>
接着是Web服务暴露的接口及其实现类
package com.jadyer.server; public interface HelloService { public String sayHello(String message); } package com.jadyer.server; public class HelloServiceImpl implements HelloService { public String sayHello(String message) { return message; } }
然后是Web服务端前置的Handler类
package com.jadyer.server.handler; import org.codehaus.xfire.MessageContext; import org.codehaus.xfire.handler.AbstractHandler; /** * Web服务端前置的Handler类 */ public class ServerInHandler extends AbstractHandler { public void invoke(MessageContext ctx) throws Exception { System.out.println("ServerInHandler is Invoked..."); ctx.setProperty("myServerParam", "ServerInHandler传递的参数"); System.out.println("ServerInHandler is Exit..."); } }
最后是Web服务端后置的Handler类
package com.jadyer.server.handler; import org.codehaus.xfire.MessageContext; import org.codehaus.xfire.handler.AbstractHandler; /** * Web服务端后置的Handler类 */ public class ServerOutHandler extends AbstractHandler { public void invoke(MessageContext ctx) throws Exception { System.out.println("ServerOutHandler is Invoked..."); System.out.println(ctx.getProperty("myServerParam")); System.out.println("ServerOutHandler is Exit..."); } }
Web服务端,至此完毕
下面是客户端代码,我这里另建一个Java Project
首先是客户端前置的Handler类
package com.jadyer.client.handler; import org.codehaus.xfire.MessageContext; import org.codehaus.xfire.handler.AbstractHandler; /** * 客户端前置的Handler类 */ public class ClientOutHandler extends AbstractHandler { public void invoke(MessageContext ctx) throws Exception { System.out.println("ClientOutHandler is Invoked..."); ctx.setProperty("myClientParam", "ClientOutHandler传递的参数"); System.out.println("ClientOutHandler is Exit..."); } }
然后是客户端后置的Handler类
package com.jadyer.client.handler; import org.codehaus.xfire.MessageContext; import org.codehaus.xfire.handler.AbstractHandler; /** * 客户端后置的Handler类 */ public class ClientInHandler extends AbstractHandler { public void invoke(MessageContext ctx) throws Exception { System.out.println("ClientInHandler is Invoked..."); System.out.println(ctx.getProperty("myClientParam")); System.out.println("ClientInHandler is Exit..."); } }
最后是客户端的测试代码
package com.jadyer.client; import java.net.MalformedURLException; 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.jadyer.client.handler.ClientInHandler; import com.jadyer.client.handler.ClientOutHandler; import com.jadyer.server.HelloService; /** * 客户端测试代码 * @see 假设此时Web服务端为我们提供接口了 * @see 此时客户端需要引入MyEclipse提供的XFire 1.2 Core、HTTP Client Libraries两个类库 * @see 其中关于客户端的ClientOutHandler.java和ClientInHandler.java是由我们在客户端上手工创建的 */ public class HandlerTest { private static final String url = "http://127.0.0.1:8080/XFire_Handler/services/XFireHandlerDemo"; private static final String namespace = "http://www.jadyer.com/XFireHandlerDemo"; public static void main(String[] args) { Service serviceModel = new ObjectServiceFactory().create(HelloService.class, null, namespace, null); try{ HelloService clientService = (HelloService)new XFireProxyFactory().create(serviceModel, url); Client client = Client.getInstance(clientService); client.addInHandler(new ClientInHandler()); client.addOutHandler(new ClientOutHandler()); System.out.println(clientService.sayHello("测试成功")); }catch(MalformedURLException e){ e.printStackTrace(); } } } /******【当运行该类进行测试时,客户端的控制台上会输出以下内容】******/ //ClientOutHandler is Invoked... //ClientOutHandler is Exit... //ClientInHandler is Invoked... //ClientOutHandler传递的参数 //ClientInHandler is Exit... //测试成功 /******【当运行该类进行测试时,服务端的控制台上会输出以下内容】******/ //ServerInHandler is Invoked... //ServerInHandler is Exit... //ServerOutHandler is Invoked... //ServerInHandler传递的参数 //ServerOutHandler is Exit... /******★★此时我们视其为Handler测试成功★★*********************/