使用hessian实现web服务接口(一)

Hessian是一个轻量级的remotingonhttp工具,采用BinaryRPC协议,适合发送二进制数据,具有防火墙穿透能力。它通过Servlet提供远程服务,可与Spring框架整合,实现远程过程调用。本文介绍了Hessian的安装配置、开发流程及示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Hessian介绍
Hessian是一个轻量级的remoting on http工具,采用的是Binary RPC协议,所以它很适合于发送二进制数据,同时又具有防火墙穿透能力。Hessian一般是通过Web应用来提供服务,因此非常类似于平时我们用的 WebService。只是它不使用SOAP协议,但相比webservice而言更轻量级、简单、快捷。
Hessian官网:http://hessian.caucho.com/

Hessian 可通过Servlet提供远程服务,需要将匹配某个模式的请求映射到Hessian服务。也可Spring框架整合,通过它的 DispatcherServlet可以完成该功能,DispatcherServlet可将匹配模式的请求转发到Hessian服务。Hessian的server端提供一个servlet基类, 用来处理发送的请求,而Hessian的这个远程过程调用,完全使用动态代理来实现的,,建议采用面向接口编程,Hessian服务通过接口暴露。

Hessian处理过程示意图:客户端——>序列化写到输出流——>远程方法(服务器端)——>序列化写到输出流 ——>客户端读取输入流——>输出结果

具体Hessian的开发:
 

创建HessianService接口

public interface HessianService {
	 public String sayHello(String name);
}

创建实现类HessianServiceImpl

public class HessianServiceImpl implements HessianService{

	@Override
	public String sayHello(String name) {
		return "hello,"+name;
	}

}

pom.xml中添加hessian依赖

<!-- hessian -->
<dependency>
    <groupId>com.caucho</groupId>
    <artifactId>hessian</artifactId>
    <version>4.0.7</version>
</dependency>

配置web.xml

 <servlet>
  		<!-- 配置 HessianServlet,Servlet的名字随便配置,例如这里配置成ServiceServlet-->
	  	<servlet-name>hessianServiceTest</servlet-name>
	  	 <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
	  	<load-on-startup>1</load-on-startup>
	  	<init-param>
            <!-- 配置服务的接口,参数名称service-class或其他固定写法(下面贴上源码),如果不是这种写法,会抛ServletException异常提示server must extend HessianServlet -->
	  		<param-name>service-class</param-name>
	  		<param-value>com.test.hessian.HessianServiceImpl</param-value>
	  	</init-param>
  </servlet>
  <servlet-mapping>
	  	<!-- 映射 HessianServlet的访问URL地址-->
	  	<servlet-name>hessianServiceTest</servlet-name>
	  	<url-pattern>/hs/service</url-pattern>
  </servlet-mapping>

Hessian部分源码

public void init(ServletConfig config)
/*     */     throws ServletException
/*     */   {
/* 200 */     super.init(config);
/*     */     try
/*     */     {
/* 203 */       if (this._homeImpl == null)
/*     */       {
/* 205 */         if (getInitParameter("home-class") != null) {
/* 206 */           String className = getInitParameter("home-class");
/*     */           
/* 208 */           Class homeClass = loadClass(className);
/*     */           
/* 210 */           this._homeImpl = homeClass.newInstance();
/*     */           
/* 212 */           init(this._homeImpl);
/*     */         }
/* 214 */         else if (getInitParameter("service-class") != null) {
/* 215 */           String className = getInitParameter("service-class");
/*     */           
/* 217 */           Class homeClass = loadClass(className);
/*     */           
/* 219 */           this._homeImpl = homeClass.newInstance();
/*     */           
/* 221 */           init(this._homeImpl);
/*     */         }
/*     */         else {
/* 224 */           if (getClass().equals(HessianServlet.class)) {
/* 225 */             throw new ServletException("server must extend HessianServlet");
/*     */           }
/* 227 */           this._homeImpl = this;
/*     */         }
/*     */       }
/* 230 */       if (this._homeAPI == null)
/*     */       {
/* 232 */         if (getInitParameter("home-api") != null) {
/* 233 */           String className = getInitParameter("home-api");
/*     */           
/* 235 */           this._homeAPI = loadClass(className);
/*     */         }
/* 237 */         else if (getInitParameter("api-class") != null) {
/* 238 */           String className = getInitParameter("api-class");
/*     */           
/* 240 */           this._homeAPI = loadClass(className);
/*     */         }
/* 242 */         else if (this._homeImpl != null) {
/* 243 */           this._homeAPI = findRemoteAPI(this._homeImpl.getClass());
/*     */           
/* 245 */           if (this._homeAPI == null)
/* 246 */             this._homeAPI = this._homeImpl.getClass();
/*     */         }
/*     */       }
/* 249 */       if (this._objectImpl == null)
/*     */       {
/* 251 */         if (getInitParameter("object-class") != null) {
/* 252 */           String className = getInitParameter("object-class");
/*     */           
/* 254 */           Class objectClass = loadClass(className);
/*     */           
/* 256 */           this._objectImpl = objectClass.newInstance();
/*     */           
/* 258 */           init(this._objectImpl);
/*     */         }
/*     */       }
/* 261 */       if (this._objectAPI == null)
/*     */       {
/* 263 */         if (getInitParameter("object-api") != null) {
/* 264 */           String className = getInitParameter("object-api");
/*     */           
/* 266 */           this._objectAPI = loadClass(className);
/*     */         }
/* 268 */         else if (this._objectImpl != null) {
/* 269 */           this._objectAPI = this._objectImpl.getClass();
/*     */         } }
/* 271 */       this._homeSkeleton = new HessianSkeleton(this._homeImpl, this._homeAPI);
/* 272 */       if (this._objectAPI != null) {
/* 273 */         this._homeSkeleton.setObjectClass(this._objectAPI);
/*     */       }
/* 275 */       if (this._objectImpl != null) {
/* 276 */         this._objectSkeleton = new HessianSkeleton(this._objectImpl, this._objectAPI);
/* 277 */         this._objectSkeleton.setHomeClass(this._homeAPI);
/*     */       }
/*     */       else {
/* 280 */         this._objectSkeleton = this._homeSkeleton;
/*     */       }
/* 282 */       if ("true".equals(getInitParameter("debug"))) {
/* 283 */         this._isDebug = true;
/*     */       }
/* 285 */       if ("false".equals(getInitParameter("send-collection-type")))
/* 286 */         setSendCollectionType(false);
/*     */     } catch (ServletException e) {
/* 288 */       throw e;
/*     */     } catch (Exception e) {
/* 290 */       throw new ServletException(e);
/*     */     }
/*     */   }


访问:http://localhost:8080/web-socket/hs/service

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值