学习了以下Hession使用,下面说明一下如何使用(网上有先人弄过例子的),这里仅仅个人学习交流使用
1.需要JAR
hessian-3.2.1.jar(JAVA 版本的)
2.代码
服务端接口
IBasic.java
package hessian;
import java.util.List;
import java.util.Map;
/***********************************************************************
*
* IBasic.java
* @copyright Copyright: 2009-2012
* @creator 周辉<br/>
* @create-time May 21, 2009 3:38:06 PM
* @revision $Id: *
***********************************************************************/
public interface IBasic {
/**
* 测试字符串
* @return
*/
public String hello();
/**
* 取一辆汽车 测试对象传递
* @return
*/
public Car getCar();
/**
* 取得list 数据
* @return
*/
public List<String> getList();
/**
* 取得map 数据
* @return
*/
public Map<String,String> getMap();
/**
* 保存数据
* @param map
*/
public void setMap(Map<String,Car> map);
}
服务端实现类代码 BasicService.java
package hessian;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/*******************************************************************************
*
* BasicService.java
*
* @copyright Copyright: 2009-2012
* @creator 周辉<br/>
* @create-time May 21, 2009 3:39:21 PM
* @revision $Id: *
******************************************************************************/
public class BasicService implements IBasic {
/**
* 取一辆汽车 测试对象传递
*
* @return
*/
public Car getCar() {
Car car = new Car();
car.setColor("RED红色");
car.setLength("2400");
car.setName("HAHACHE");
return car;
}
/**
* 测试字符串
*
* @return
*/
public String hello() {
return "hello zhouhui";
}
/**
* 取得list 数据
*
* @return
*/
public List<String> getList() {
List<String> list = new ArrayList<String>();
list.add("1");
list.add("2");
list.add("3");
return list;
}
/**
* 取得map 数据
*
* @return
*/
public Map<String, String> getMap() {
Map<String, String> map = new HashMap<String, String>();
if (map.isEmpty()) {
map.put("1", "zhouhui1");
map.put("2", "zhouhui2");
map.put("3", "zhouhui3");
}
return map;
}
/**
* 保存数据
*
* @param map
*/
public void setMap(Map<String, Car> map) {
for (Map.Entry<String, Car> item : map.entrySet()) {
System.out.println("mapkey: " + item.getKey());
System.out.println("mapvalue: " + item.getValue().getColor());
System.out.println("mapvalue: " + item.getValue().getName());
}
}
}
一个封装的JAVABEAN
Car.java
package hessian;
import java.io.Serializable;
/***********************************************************************
*
* Car.java
* @copyright Copyright: 2009-2012
* @creator 周辉<br/>
* @create-time May 21, 2009 3:38:21 PM
* @revision $Id: *
***********************************************************************/
public class Car implements Serializable {
private String color;
private String length;
private String name;
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getLength() {
return length;
}
public void setLength(String length) {
this.length = length;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
配置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>HessianServlet</servlet-name>
<servlet-class>
com.caucho.hessian.server.HessianServlet
</servlet-class>
<init-param>
<param-name>home-class</param-name>
<param-value>hessian.BasicService</param-value>
</init-param>
<init-param>
<param-name>home-api</param-name>
<param-value>hessian.IBasic</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>HessianServlet</servlet-name>
<url-pattern>/hessianServlet</url-pattern>
</servlet-mapping>
</web-app>
最后客户端调用的代码
testClient.java
package hessian;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.caucho.hessian.client.HessianProxyFactory;
/***********************************************************************
*
* testClient.java
* @copyright Copyright: 2009-2012
* @creator 周辉<br/>
* @create-time May 21, 2009 3:44:45 PM
* @revision $Id: *
***********************************************************************/
public class testClient {
/**
* @param args
*/
public static void main(String[] args)throws Exception {
// TODO Auto-generated method stub
String url = "http://127.0.0.1:8081/testHession/hessianServlet";
HessianProxyFactory factory = new HessianProxyFactory();
IBasic basic = (IBasic) factory.create(IBasic.class, url);
Car car = basic.getCar();
// 客户端调用服务端list 数据
List<String> list =basic.getList();
for (String s :list){
System.out.println("list: " + s);
}
System.out.println("Hello: " + basic.hello());
System.out.println("Hello: " + car.getName());
// 客户端调用服务端MAP 数据
Map<String,String> map =basic.getMap();
for(Map.Entry<String,String> item:map.entrySet()){
System.out.println("mapkey: " +item.getKey());
System.out.println("mapvalue: " +item.getValue());
}
// 客户端向服务端发送数据
Map<String,Car> map2 = new HashMap<String,Car>();
if(map2.isEmpty()){
Car c = new Car();
c.setColor("red");
c.setLength("111");
c.setName("zhouhui");
map2.put("1", c);
map2.put("2", c);
map2.put("3", c);
}
basic.setMap(map2);
}
}
最后启动TOMCAT,发布好服务后,启动testClient.java 调用服务代码
客户端控制台打出调用服务的输出结果
在从TOMCAT 服务控制台查看客户端口输入数据
好了一切OK 了
注意点:
自定义JAVABEAN 一定要序列化
implements Serializable
否则会报错
Caused by: java.io.IOException: Server returned HTTP response code: 500 for URL: http://127.0.0.1:8081/testHession/hessianServlet
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1296)
at java.security.AccessController.doPrivileged(Native Method)
at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1290)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:944)
at com.caucho.hessian.client.HessianProxy.invoke(HessianProxy.java:184)