首先强调这里是SpringMVC,不是spring,这二者在集成Hessian的时候仍是有差异的。Spring集成相对简单,网上随便搜一个就行。java
SpringMVC有点麻烦。web
注:若是你还不了解Hessian,能够看Hessian简单示例spring
假设你的SpringMVC环境已经配置了好了。浏览器
主要是在web.xml中有了以下的配置:服务器
<servlet> <!-- 名字随便,和你springmvc的配置xml一致便可 --> <servlet-name>sys</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <!-- 你本身的映射配置 --> </servlet-mapping>
另外你已经配置了相应的sys-servlet.xml
文件(sys
名字和你本身的保持一致便可)。mvc
为了针对Hessian的请求,在web.xml
中增长了以下映射:app
<servlet-mapping> <!-- 名字要保持一致 --> <servlet-name>sys</servlet-name> <url-pattern>*.hessian</url-pattern> </servlet-mapping>
而后在sys-servlet.xml
中添加以下配置:url
<!--hessian--> <bean id="httpRequestHandlerAdapter" class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"/> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> <bean id="importService" class="com.xxx.pr.service.impl.ImportServiceImpl"/> <bean name="/import.hessian" class="org.springframework.remoting.caucho.HessianServiceExporter"> <property name="service" ref="importService"/> <property name="serviceInterface" value="com.xxx.pr.service.ImportService"/> </bean>
HessianServiceExporter
是继承HttpRequestHandler
接口的,因此须要HttpRequestHandlerAdapter
来处理这种请求。spa
BeanNameUrlHandlerMapping
的做用是,当<bean>
的name
属性以/
开头的时候,映射为url请求。.net
HessianServiceExporter
中的两项属性,一个是service
,ref
属性指向的实现类。一个是serviceInterface
,指向的是接口。
作好如上配置后,启动服务器。
而后访问http://localhost:8080/myweb/import.hessian便可。具体地址根据本身的来写。
在浏览器打开后,会显示下面的样子:
type Status report
message HessianServiceExporter only supports POST requests
description The specified HTTP method is not allowed for the requested resource.
Apache Tomcat/7.0.56
若是如上显示,说明就没有问题了。
<bean id="importBean" class="org.springframework.remoting.caucho.HessianProxyFactoryBean"> <property name="serviceUrl" value="http://localhost:8080/myweb/import.hessian"></property> <property name="serviceInterface" value="com.xxx.pr.service.ImportService"></property> </bean>
String url = "http://localhost:8080/myweb/import.hessian"; HessianProxyFactory factory = new HessianProxyFactory(); ImportService basic = (ImportService) factory.create(ImportService.class, url);