Hessian技术在java中的使用
最近公司接到一个新的项目,其中需要开发一个服务端和客户端,两个项目之间进行数据交互,其中包括二进制数据,相比WebService,Hessian更简单、快捷。采用的是二进制RPC协议,因为采用的是二进制协议,所以它很适合于发送二进制数据。因此Hessian作为首选应用到项目中,以下简单介绍一下Hessian的使用,搭建一个简单而又万能的hellowrold小例!
相关的理论知识这里就不唠叨了。。。
开整。。。
整体步骤分为以下三步:
一、准备hessian的jar包;
二、服务端开发(开发一个简单的接口,在web.xml中进行配置);
三、客户端开发(导入jar包;开发一个和服务端一样的接口;远程调用服务端接口);
步骤一(准备jar包):
准备jar包,本文最下方有我已经上传好的demo,里面包含jar包,或者自行下载hessian.jar;
步骤二(服务端开发):
新建一个名EasyHession的webProject项目,将Hessian.jar放入web-inf下的lib中
创建接口:
package app.demo;
public interface BasicAPI {
public void setGreeting(String greeting);
public String hello();
public User getUser();
}
实现接口:
package app.demo;
public class BasicService extends HessianServlet implements BasicAPI {
private String _greeting = "Hello, world";
public void setGreeting(String greeting)
{
_greeting = greeting;
System.out.println("set greeting success:"+_greeting);
}
public String hello()
{
return _greeting;
}
public User getUser() {
return new User("prance", "meshow");
}
}
创建一个实现Serializable的projo类也可以是Bean。
package app.demo;
import java.io.Serializable;
public class User implements Serializable{
String userName ="snoopy";
String password ="showme";
public User(Stringuser, String pwd) {
this.userName =user;
this.password= pwd;
}
public String getUserName() {
return userName;
}
public String getPassword() {
return password;
}
}
接下来是配置web.xml
<?xml version="1.0"encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
<init-param>
<param-name>service-class</param-name>
<param-value>app.demo.BasicService</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
为了测试可以在编辑一个简单的test.jsp
<%@ pageimport="com.caucho.hessian.client.HessianProxyFactory,
app.demo.BasicAPI"%>
<%@page language="java"%>
<%
HessianProxyFactory factory = newHessianProxyFactory();
String url = ("http://" +request.getServerName() + ":" +request.getServerPort() +
request.getContextPath() + "/hello");
out.println(url);
BasicAPI basic = (BasicAPI) factory.create(BasicAPI.class,url);
out.println("Hello: " + basic.hello());
out.println("Hello: " + basic.getUser() .getUserName() );
out.println("Hello: " +basic.getUser().getPassword() );
%>
步骤三(客户端开发):
创建一个名为HessianClient的JAVAProject,载入hessian.jar包
创建与服务器端一样的接口,及基础类
public interface BasicAPI{
public void setGreeting(String greeting);
public String hello();
public User getUser();
}
import java.io.Serializable;
public class User implements Serializable{
String userName ="snoopy";
String password ="showme";
public User(Stringuser, String pwd) {
this.userName =user;
this.password= pwd;
}
public String getUserName() {
return userName;
}
public String getPassword() {
return password;
}
}
创建客户端程序
importcom.caucho.hessian.client.HessianProxyFactory;
public class BasicClient {
public static void main(String[] args)
throwsException
{
String url ="http://127.0.0.1:8080/Hessian/hello";
HessianProxyFactory factory = new HessianProxyFactory();
BasicAPI basic = (BasicAPI) factory.create(BasicAPI.class, url);
System.out.println("Hello:" + basic.hello());
System.out.println("Hello:" + basic.getUser().getUserName());
System.out.println("Hello:" + basic.getUser().getPassword());
basic.setGreeting("HelloGreeting");
System.out.println("Hello:" + basic.hello());
}
}
运行结果:
Hello: Hello, world
Hello: prance
Hello: meshow
Hello: Hello Greeting
我的demo源码(在Myeclipse8.6环境下开发),进入如下地址进行下载:
http://download.csdn.net/download/xiaoqi270620903/7349287