1、去官网下载hession的java架包:
官网地址:http://hessian.caucho.com/
我这里下载的是hessian-4.0.7.jar
2、编写接口:
package com.abin.lee.hession;
public interface IBasicSource {
public String getMessage(String name);
public UserBean getUserBean();
}
3、编写接口实现类:
package com.abin.lee.hession;
public class BasicSource implements IBasicSource{
public String getMessage(String name) {
return "hello,"+name;
}
public UserBean getUserBean() {
UserBean userBean=new UserBean();
userBean.setId(1);
userBean.setAddress("liangcheng");
userBean.setName("lee");
return userBean;
}
}
4、实体类,这里需要注意一下,这里必须的实现序列化接口Serializable,因为是需要网络传输的
package com.abin.lee.hession;
import java.io.Serializable;
public class UserBean implements Serializable{
private static final long serialVersionUID = -8931696839145847364L;
private int id;
private String name;
private String address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
5、配置web.xml
<!--下面是Hession的配置-->
<servlet>
<servlet-name>hessianServlet</servlet-name>
<servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
<init-param>
<param-name>home-class</param-name>
<param-value>com.abin.lee.hession.BasicSource</param-value>
</init-param>
<init-param>
<param-name>home-api</param-name>
<param-value>com.abin.lee.hession.IBasicSource</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>hessianServlet</servlet-name>
<url-pattern>/hessianServlet</url-pattern>
</servlet-mapping>
6、编写测试类:package com.abin.lee.hession.test;
import java.net.MalformedURLException;
import junit.framework.TestCase;
import com.abin.lee.hession.IBasicSource;
import com.abin.lee.hession.UserBean;
import com.caucho.hessian.client.HessianProxyFactory;
public class TestHession extends TestCase{
private static final String HESSION_URL="http://localhost:8081/universal/hessianServlet";
public void testHession() throws MalformedURLException{
HessianProxyFactory factory=new HessianProxyFactory();
IBasicSource basic=(IBasicSource)factory.create(IBasicSource.class, HESSION_URL);
String result=basic.getMessage("abin");
System.out.println("result="+result);
UserBean user=basic.getUserBean();
System.out.println("ID="+user.getId()+",NAME="+user.getName()+",ADDRESS="+user.getAddress());
}
}
测试结果:
result=hello,abin
ID=1,NAME=lee,ADDRESS=liangcheng
到了这里就成功了。