由于本人刚开始接触hession,还未深入的了解hession,所以一下的代码没有注释。
使用工具 : eclipse 3.3 tomcat 6.0.21 JDK 1.6
jar包:hessian-4.0.7.jar(见附件)
1、在 eclipse 中新建一个 Dynamic Web Project,名为 hession
2、在src下建包:com.hession
3、创建的代码如下:
package com.hession; import java.io.Serializable; public class Car implements Serializable { /** * */ private static final long serialVersionUID = -8470305814077115888L; private String color; private String length; private String name; public String toString(){ StringBuffer buf = new StringBuffer(); buf.append("[Car:{name=").append(getName()); buf.append(",color=").append(getColor()); buf.append(",length=").append(getLength()); buf.append("}]"); return buf.toString(); } //这里省略 setter()/getter() }
接口:
package com.hession; public interface IBasic { public String hello(); public Car getCar(); }
接口的实现类
package com.hession; public class BasicService implements IBasic { private String hello = "hello,world!"; @Override public Car getCar() { Car car = new Car(); car.setColor("RED红色"); car.setLength("2400"); car.setName("HAHACHE"); return car; } @Override public String hello() { return hello; } }
测试:
package com.hession; import java.net.MalformedURLException; import com.caucho.hessian.client.HessianProxyFactory; public class BasicClient { /** * @param args */ public static void main(String[] args) { String url = "http://localhost:8080/hession/hello"; HessianProxyFactory f = new HessianProxyFactory(); try { IBasic basic = (IBasic)f.create(IBasic.class, url); Car car = basic.getCar(); System.out.println("hello:"+basic.hello()); System.out.println(car); } catch (MalformedURLException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } }
f.create(IBasic.class, url)中,第一个参数必须是接口,不能是实现类。
url 中的端口号以自己 tomcat 中配置的端口号为准。
web.xml
<servlet> <servlet-name>hello</servlet-name> <servlet-class> com.caucho.hessian.server.HessianServlet </servlet-class> <init-param> <param-name>home-class</param-name> <param-value>com.hession.BasicService</param-value> </init-param> <init-param> <param-name>home-api</param-name> <param-value>com.hession.IBasic</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping>
4、测试步骤
如果此时直接运行 BasicClient 类中的main方法,将会出现以下的异常:
com.caucho.hessian.client.HessianRuntimeException: java.net.ConnectException: Connection refused: connect at com.caucho.hessian.client.HessianProxy.sendRequest(HessianProxy.java:268) at com.caucho.hessian.client.HessianProxy.invoke(HessianProxy.java:170) at $Proxy0.getCar(Unknown Source) at com.hession.BasicClient.main(BasicClient.java:17) Caused by: java.net.ConnectException: Connection refused: connect at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333) at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366) at java.net.Socket.connect(Socket.java:529) at java.net.Socket.connect(Socket.java:478) at sun.net.NetworkClient.doConnect(NetworkClient.java:163) at sun.net.www.http.HttpClient.openServer(HttpClient.java:394) at sun.net.www.http.HttpClient.openServer(HttpClient.java:529) at sun.net.www.http.HttpClient.<init>(HttpClient.java:233) at sun.net.www.http.HttpClient.New(HttpClient.java:306) at sun.net.www.http.HttpClient.New(HttpClient.java:323) at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:860) at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:801) at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:726) at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:904) at com.caucho.hessian.client.HessianURLConnection.getOutputStream(HessianURLConnection.java:94) at com.caucho.hessian.client.HessianProxy.sendRequest(HessianProxy.java:266) ... 3 more
原因是我们还没有启动 tomcat server。
4.1 将工程 hession 添加到 eclipse 中的 tomcat server(见附件中的图片 1.bmp)
4.2 启动 tomcat server
4.3 运行 BasicClient 类中的main方法
运行成功,运行结果如下:
hello:hello,world! [Car:{name=HAHACHE,color=RED红色,length=2400}]