在 Spring Web Flow 项目中应用 Hessian 服务

原来作的一个项目因为页面跳转比较多,应用了Spring Web Flow 项目作视图的页面流转控制,关于Spring Web Flow的介绍可以参考我的另一篇文章:[url]http://lib.iteye.com/blog/299142[/url],最近需要在项目中引用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的请求分配相应的控制器才可以正确的响应。

以下[url="http://lib.iteye.com/blog/299142"]Spring Web Flow中介绍的例子[/url]集成 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 对应的控制器:

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean
id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView">
</property>
<property name="prefix" value="/WEB-INF/jsp/">
</property>
<property name="suffix" value=".jsp">
</property>
</bean>
<bean
id="viewMappings"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<!-- /shopping.do 请求由 flowController 来处理 -->
<property name="mappings">
<value> /shopping=flowController </value>

<!-- hessian 测试实例 为Hessian的Url请求分配对应的处理器 -->
<value> /webServiceHessianSample=webServiceHessianExporter </value>

</property>
<property name="defaultHandler">
<!-- UrlFilenameViewController 会将 "/index" 这样的请求映射成名为 "index" 的视图 -->
<bean class="org.springframework.web.servlet.mvc.UrlFilenameViewController" />
</property>
</bean>
<bean
id="flowController"
class="org.springframework.webflow.mvc.servlet.FlowController">
<property name="flowExecutor" ref="flowExecutor"/>
</bean>

<!-- hessian 测试实例 ServiceBean 的定义 -->
<bean id="webServiceHessianSample" class="samples.hessian.WebServiceSampleImpl"></bean>
<bean name="webServiceHessianExporter"
class="org.springframework.remoting.caucho.HessianServiceExporter">
<property name="service" ref="webServiceHessianSample" />
<property name="serviceInterface" value="samples.hessian.WebServiceSample" />
</bean>

</beans>


单元测试代码:

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 的下载地址是:[url]http://hessian.caucho.com/[/url]

例子源代码见附件
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值