这两种实现都是基于http的,只不过Hessian使用二进制格式,Burlap使用XML格式传输文件。在4.x的Spring中官方已经将Burlap做为废弃类,配置和Hessian相同,下面详细讲下HessianServiceExporterjava
咱们首先先试着写下运行程序web
服务端
spring
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的拦截器)就必须本身实现或者使用第二种方式
mvc
web.xml中添加,由于首先是Springmvc先启动,因此设置较后一些
app
<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程序须要这两个包
maven
客户端ide
<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的访问,因此必需要要加入
post
当咱们用客户端请求的时候会发现出现了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集成了