原来作的一个项目因为页面跳转比较多,应用了Spring Web Flow 项目作视图的页面流转控制,关于Spring Web Flow的介绍可以参考我的另一篇文章: http://lib.iteye.com/blog/299142,最近需要在项目中引用WebService 远程服务的机制供其他系统调用,比较了一些这方面的开源项目,发现Hessian配置简单,性能优异,决定集成Hessian到这个项目中。
本来Hessaion的配置是比较简单的,很多文章都有介绍,主要是在web.xml中配置Servlet,然后配置ServiceBean就可以了,这方面的文章Google一下会很多,不在这里介绍。我的项目中因为用到了Spring Web Flow,通过org.springframework.web.servlet.DispatcherServlet为 Spring Web Flow 控制器作了定义,与Hessian的servlet造成了冲突,使Hessian不能正常工作。经过研究发现需要通过Spring Web Flow的 UrlHandler 对Hessian的请求分配相应的控制器才可以正确的响应。
以下 Spring Web Flow中介绍的例子集成 Hessian 的例子:
服务端代码:
package samples.hessian; public interface WebServiceSample { public String say(String hello); }
package samples.hessian; public class WebServiceSampleImpl implements WebServiceSample{ public String say(String hello) { return hello + " world!"; } }
修改webmvc-config.xml文件,增加ServiceBean的定义,及 Hessian 对应的控制器:
/shopping=flowController /webServiceHessianSample=webServiceHessianExporter
单元测试代码:
package test.sample; import java.net.MalformedURLException; import junit.framework.Assert; import junit.framework.TestCase; import cn.org.coral.sample.hessian.WebServiceSample; import com.caucho.hessian.client.HessianProxyFactory; public class TestHessian extends TestCase { public void testSay() throws MalformedURLException { String url = "http://localhost:8080/coral/spring/webServiceHessianSample"; HessianProxyFactory factory = new HessianProxyFactory(); WebServiceSample sample = (WebServiceSample) factory.create( WebServiceSample.class, url); //System.out.print(sample.say("hello")); Assert.assertEquals(sample.say("hello"), "hello world!"); } }
另外,需要注意的地方,我使用的是 hessian 版本是hessian-3.1.5.jar,使用最新的有问题,可能api发生了变化,还未仔细研究。hessian 的下载地址是: http://hessian.caucho.com/
例子源代码见附件