这两种实现都是基于http的,只不过Hessian使用二进制格式,Burlap使用XML格式传输文件。在4.x的Spring中官方已经将Burlap作为废弃类,配置和Hessian相同,下面详细讲下HessianServiceExporter
我们首先先试着写下运行程序
服务端
rmi.xml
<bean id="accountService" class="example.AccountServiceImpl"> </bean> <bean name="accountExporter" class="org.springframework.remoting.caucho.HessianServiceExporter"> <property name="service" ref="accountService"/> <property name="serviceInterface" value="example.AccountService"/> </bean> <!-- 也可以用下面的方法 --> <bean name="/AccountService" class="org.springframework.remoting.caucho.HessianServiceExporter"> <property name="service" ref="accountService"/> <property name="serviceInterface" value="example.AccountService"/> </bean>
上面两种方法主要是因为Spring mvc的BeanNameUrlHandlerMapping会自动将其处理,如果使用其他框架(例如实现Struts2的拦截器)就必须自己实现或者使用第二种方式
web.xml中添加,因为首先是Springmvc先启动,所以设置较后一些
<servlet> <servlet-name>accountExporter</servlet-name> <servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>accountExporter</servlet-name> <url-pattern>/remoting/*</url-pattern> </servlet-mapping>
并在maven中加入依赖
<dependency> <groupId>com.caucho</groupId> <artifactId>hessian</artifactId> <version>4.0.38</version> </dependency>
记得一定要添加spring-webmvc和spring-web,否则无法运行,因为web程序需要这两个包
客户端
<bean id="accountService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean"> <property name="serviceUrl" value="http://localhost:8080/remoting/AccountService"/> <property name="serviceInterface" value="example.AccountService"/> </bean>
同样要添加spring-web和hessian依赖,只是比上边少了mvc包而已,它不需要公开接口,web包包含了client的访问,所以必须要要加入
当我们用客户端请求的时候会发现出现了RMI Test!
当我们用浏览器的get方法请求会出现如下信息:
type Status report
message HessianServiceExporter only supports POST requests
description The specified HTTP method is not allowed for the requested resource.
必须是post。我们来看下HessianServiceExporter的实现
public class HessianServiceExporter extends HessianExporter implements HttpRequestHandler { /** * Processes the incoming Hessian request and creates a Hessian response. */ @Override public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //判断方法,是否是post如果不是则相应错误消息,HessianServiceExporter only supports POST requests if (!"POST".equals(request.getMethod())) { throw new HttpRequestMethodNotSupportedException(request.getMethod(), new String[] {"POST"}, "HessianServiceExporter only supports POST requests"); } //设置请求类型为application/x-hessian response.setContentType(CONTENT_TYPE_HESSIAN); try { //这里主要就是执行调用导出的对象。 invoke(request.getInputStream(), response.getOutputStream()); } catch (Throwable ex) { throw new NestedServletException("Hessian skeleton invocation failed", ex); } } }
同样我们在浏览器调用post方法,则会抛出异常。
那么Hessian的远程调用比直接调用RmiProxyFactoryBean有什么好处呢?
首先它是基于http的,所以它就有了http的所有特性,那么我们就可以对其url进行权限控制,这样,我们就可以进行控制,同时通过阅读HessianProxyFactoryBean类源码,在父类HessianClientInterceptor发现了如下代码
/** * Set the username that this factory should use to access the remote service. * Default is none. * <p>The username will be sent by Hessian via HTTP Basic Authentication. * @see com.caucho.hessian.client.HessianProxyFactory#setUser */ public void setUsername(String username) { this.proxyFactory.setUser(username); } /** * Set the password that this factory should use to access the remote service. * Default is none. * <p>The password will be sent by Hessian via HTTP Basic Authentication. * @see com.caucho.hessian.client.HessianProxyFactory#setPassword */ public void setPassword(String password) { this.proxyFactory.setPassword(password); }
最后通过阅读官方文档,其是支持spring-security,
官方给出的配置方式如下:
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"> <property name="interceptors" ref="authorizationInterceptor"/> </bean> <bean id="authorizationInterceptor" class="org.springframework.web.servlet.handler.UserRoleAuthorizationInterceptor"> <property name="authorizedRoles" value="administrator,operator"/> </bean>
到此,HessianServiceExporter的使用也就结束了,有时间在研究下和spring-security集成了