baidu百科这样介绍它:
Hessian是一个轻量级的remoting on http工具,使用简单的方法提供了RMI的功能. 相比WebService,Hessian更简单、快捷。采用的是二进制RPC协议,因为采用的是二进制协议,所以它很适合于发送二进制数据.
开元百科这样介绍它:
Hessian是Caucho开发的一种二进制Web Service协议。采取Apache许可协议.
The Hessian binary web service protocol makes web services usable without requiring a large framework, and without learning yet another alphabet soup of protocols. Because it is a binary protocol, it is well-suited to sending binary data without any need to extend the protocol with attachments.
Hessian是一个精心打造的Web Service实现,它支持Java、C#、Ruby、PHP和Python,又新增了ActionScript。使用Adobe Flash和Flex的RIA开发者们在需要集成业务服务的时候,Hessian应该是一个不错的选择。
既然都它这么神奇,那么我们就又要去认识下这位新朋友。
项目主页:
http://hessian.caucho.com/
建议大家仔细认真阅读下官方网站。
还是先从helloworld着手。
它只需要一个jar包即可
去官方下载适用于java环境下的jar包支持
hessian-4.0.7.jar hessian-4.0.7-src.jar
创建web工程。
编写接口:
package com.hessian.demo.inf;
public interface HelloWorld {
public String sayHelloWorld(String name);
}
编写实现类:
package com.hessian.demo.impl;
import com.hessian.demo.inf.HelloWorld;
public class HelloWorldImpl implements HelloWorld {
public String sayHelloWorld(String name) {
return "Hello ,"+name+"Welcome to use hessian ! ";
}
}
配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>helloworld</servlet-name>
<servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
<init-param>
<!-- 此处hom-api为固定写法,为HessianServlet中定义的参数详细可参考HessianServlet -->
<!-- 其值为接口的类长名即 包名+类名 -->
<param-name>home-api</param-name>
<param-value>com.hessian.demo.inf.HelloWorld</param-value>
</init-param>
<init-param>
<!-- 此处hom-class为固定写法,为HessianServlet中定义的参数详细可参考HessianServlet -->
<!-- 其值为实现类的类长名即 包名+类名 -->
<param-name>home-class</param-name>
<param-value>com.hessian.demo.impl.HelloWorldImpl</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>helloworld</servlet-name>
<url-pattern>/helloworld</url-pattern>
</servlet-mapping>
</web-app>
此处web.xml中配置的初始化参数具体可参考HessianServlet的init()方法
public void init(ServletConfig config)
throws ServletException
{
super.init(config);
try {
if (_homeImpl != null) {
}
else if (getInitParameter("home-class") != null) {
String className = getInitParameter("home-class");
Class homeClass = loadClass(className);
_homeImpl = homeClass.newInstance();
init(_homeImpl);
}
else if (getInitParameter("service-class") != null) {
String className = getInitParameter("service-class");
Class homeClass = loadClass(className);
_homeImpl = homeClass.newInstance();
init(_homeImpl);
}
else {
if (getClass().equals(HessianServlet.class))
throw new ServletException("server must extend HessianServlet");
_homeImpl = this;
}
if (_homeAPI != null) {
}
else if (getInitParameter("home-api") != null) {
String className = getInitParameter("home-api");
_homeAPI = loadClass(className);
}
else if (getInitParameter("api-class") != null) {
String className = getInitParameter("api-class");
_homeAPI = loadClass(className);
}
else if (_homeImpl != null) {
_homeAPI = findRemoteAPI(_homeImpl.getClass());
if (_homeAPI == null)
_homeAPI = _homeImpl.getClass();
}
if (_objectImpl != null) {
}
else if (getInitParameter("object-class") != null) {
String className = getInitParameter("object-class");
Class objectClass = loadClass(className);
_objectImpl = objectClass.newInstance();
init(_objectImpl);
}
if (_objectAPI != null) {
}
else if (getInitParameter("object-api") != null) {
String className = getInitParameter("object-api");
_objectAPI = loadClass(className);
}
else if (_objectImpl != null)
_objectAPI = _objectImpl.getClass();
_homeSkeleton = new HessianSkeleton(_homeImpl, _homeAPI);
if (_objectAPI != null)
_homeSkeleton.setObjectClass(_objectAPI);
if (_objectImpl != null) {
_objectSkeleton = new HessianSkeleton(_objectImpl, _objectAPI);
_objectSkeleton.setHomeClass(_homeAPI);
}
else
_objectSkeleton = _homeSkeleton;
if ("true".equals(getInitParameter("debug")))
_isDebug = true;
if ("false".equals(getInitParameter("send-collection-type")))
setSendCollectionType(false);
} catch (ServletException e) {
throw e;
} catch (Exception e) {
throw new ServletException(e);
}
}
编写客户端测试代码:
package com.hessian.demo.client;
import com.caucho.hessian.client.HessianProxyFactory;
import com.hessian.demo.inf.HelloWorld;
public class HelloWorldClient {
public static void main(String[] args) throws Exception{
//servlet的访问路径
String url="http://localhost:8080/hessian/helloworld";
//创建一HessianProxyFactory对象,用法与xfire很像
HessianProxyFactory proxyFactory=new HessianProxyFactory();
//获得HelloWorld的实例,传递两个参数依次为:接口.class,该servlet的访问路径
HelloWorld helloWorld=(HelloWorld) proxyFactory.create(HelloWorld.class, url);
System.out.println(helloWorld.sayHelloWorld("张三"));
}
}
效果图: