SpringMVC集成Hessian

SpringMVC集成Hessian

首先强调这里是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

为了针对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中的两项属性,一个是serviceref属性指向的实现类。一个是serviceInterface,指向的是接口。

作好如上配置后,启动服务器。

而后访问http://localhost:8080/myweb/import.hessian便可。具体地址根据本身的来写。

在浏览器打开后,会显示下面的样子:

HTTP Status 405 - HessianServiceExporter only supports POST requests

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

若是如上显示,说明就没有问题了。

调用

若是在Spring中调用,能够尝试以下配置

<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>

也能够直接使用Hessian调用

String url = "http://localhost:8080/myweb/import.hessian";
HessianProxyFactory factory = new HessianProxyFactory();
ImportService basic = (ImportService) factory.create(ImportService.class, url);
相关文章
相关标签/搜索