Hessian介绍:
Hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能。 相比WebService,Hessian更简单、快捷。采用的是二进制RPC协议,因为采用的是二进制协议,所以它很适合于发送二进制数据。
- Maven创建项目
- pom.xml
引入jar包
<dependency> <groupId>com.caucho</groupId> <artifactId>hessian</artifactId> <version>4.0.7</version> </dependency>
3.编写entity
注意:必须implements Serializable,因为Hessian是基于二进制的RPC调用。
public class HelloWorld implements Serializable { private String name; public HelloWorld() { } public HelloWorld(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
4.编写Service
public interface HessianService { public HelloWorld sayHelloWorld(); }
5.编写Service实现类
public class HessianServiceImpl implements HessianService { public HelloWorld sayHelloWorld() { return new HelloWorld("hello,World"); } }
6.配置web.xml
<servlet> <!-- 配置 HessianServlet,Servlet的命名任意--> <servlet-name>ServiceServlet</servlet-name> <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class> <!-- 配置接口的具体实现类 ,param-name命名任意--> <init-param> <param-name>service-class</param-name> <param-value>com.xin.HessianServiceImpl</param-value> </init-param> </servlet> <!-- 映射 HessianServlet的访问URL地址--> <servlet-mapping> <servlet-name>ServiceServlet</servlet-name> <url-pattern>/ServiceServlet</url-pattern> </servlet-mapping>
7.启动tomcat
8.client测试,编写HessianTestClient
public class HessianTestClient { public static void main(String[] args) { //在服务器端的web.xml文件中配置的HessianServlet映射的访问URL地址 String url = "http://127.0.0.1:8080/ServiceServlet"; HessianProxyFactory factory = new HessianProxyFactory(); HessianService service = null; try { service = (HessianService) factory.create(HessianService.class, url); } catch (MalformedURLException e) { e.printStackTrace(); } //创建IService接口的实例对象 HelloWorld helloWorld = service.sayHelloWorld(); //调用Hessian服务器端的ServiceImpl类中的getUser方法来获取一个User对象 System.out.println(helloWorld.getName()); } }
控制台输出:
参考:http://blog.csdn.net/wodediqizhang/article/details/51605441