原来作的一个项目因为页面跳转比较多,应用了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 对应的控制器:
<?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 的下载地址是:
http://hessian.caucho.com/
例子源代码见附件
分享到:
相关推荐
第18章:讲解了Spring Web Flow和JavaServer Faces(JSF)的集成使用,展示了如何构建具有复杂流程的Web应用程序。 第19章:探讨了Spring的测试支持,包括单元测试和集成测试的策略和实现。 第20章:介绍了Spring...
第9章 在Spring中建立契约优先Web服务 9.1 介绍Spring-WS 9.2 定义契约(首先!) 9.3 使用服务端点处理消息 9.3.1 建立基于JDOM消息的端点 9.3.2 序列化消息载荷 9.4 合并在一起 9.4.1 Spring-WS:全景视图 ...
本章通过一个具体的示例应用程序,展示了Spring框架在实际项目中的应用。读者将看到如何利用Spring来构建一个完整的、具有业务逻辑的应用系统,包括数据访问、业务逻辑、Web展示等各个层面的实现。 ### 第4章:介绍...
第9章 在Spring中建立契约优先Web服务 9.1 介绍Spring-WS 9.2 定义契约(首先!) 9.3 使用服务端点处理消息 9.3.1 建立基于JDOM消息的端点 9.3.2 序列化消息载荷 9.4 合并在一起 9.4.1 Spring-WS:全景视图 ...
* Spring web: Spring MVC, Spring Web Flow 2, Spring Roo, other dynamic scripting, integration with popular Grails Framework (and Groovy), REST/web services, and more. This book guides you step by ...
- **Spring Web Flow**:Spring Web Flow 是一个针对复杂交互式 Web 流程的框架,它可以轻松处理复杂的用户交互过程。 - **Spring Remoting**:Spring 提供了多种远程调用技术的支持,包括 RMI、HTTP Invoker、...
8.1. Configuring Web Flow in Spring 8.1.1. Wiring a flow executor 8.1.2. Configuring a flow registry 8.1.3. Handling flow requests 8.2. The components of a flow 8.2.1. States 8.2.2. Transitions 8.2.3....
这份技术文档将带你深入了解Dubbo的核心概念、功能特性以及如何在实际项目中应用。 1. **服务提供者与消费者**: - **服务提供者(Provider)**:提供服务的实体,它可以是一个应用、一个服务接口或其实现。 - **...