一、hessian的maven信息:

[html]  ​​view plain​​  ​​copy​​

 ​​print​

​​?​


  1. <dependency>  
  2. <groupId>com.caucho</groupId>  
  3. <artifactId>hessian</artifactId>  
  4. <version>4.0.38</version>  
  5. </dependency>  


二、入门示例,以web方式提供对外接口

(1)服务接口:

[java]  ​​view plain​​  ​​copy​​

 ​​print​

​​?​

  1. package com.tianjunwei.hessian.server;  
  2.   
  3. public interface HelloService {  
  4. public String helloWorld(String message);  
  5. }  

(2)接口实现:

[java]  ​​view plain​​  ​​copy​​

 ​​print​

​​?​

  1. package com.tianjunwei.hessian.server;  
  2.   
  3. public class HelloServiceImpl implements HelloService{  
  4.   
  5. @Override  
  6. public String helloWorld(String message) {  
  7. return "hello," + message;  
  8.     }  
  9. }  

(3)web.xml的配置:

[html]  ​​view plain​​  ​​copy​​

 ​​print​


  1. <web-app>  
  2. <servlet>  
  3. <servlet-name>hessian-service</servlet-name>  
  4.            
  5. <servlet-class>  
  6.              com.caucho.hessian.server.HessianServlet  
  7. </servlet-class>  
  8.            
  9. <init-param>              
  10. <param-name>home-class</param-name>              
  11. <param-value>  
  12. <!-- 服务实现类 -->  
  13.                  com.tianjunwei.hessian.server.HelloServiceImpl  
  14. </param-value>  
  15. </init-param>  
  16.    
  17. <init-param>              
  18. <param-name>home-api</param-name>  
  19. <!-- 服务接口 -->  
  20. <param-value>com.tianjunwei.hessian.server.HelloService</param-value>  
  21. </init-param>  
  22.    
  23. </servlet>  
  24.    
  25. <servlet-mapping>  
  26. <servlet-name>hessian-service</servlet-name>  
  27. <url-pattern>/hessian</url-pattern>  
  28. </servlet-mapping>  
  29. </web-app>  

(4)客户端调用:

第(3)步中,通过配置servlet,这样就可以通过发布的http进行访问,根据Hessian提供的机制,这样就可以通过url进行服务调用。

[java]  ​​view plain​​  ​​copy​​

 ​​print​

​​?​


  1. package com.tianjunwei.hessian.client;  
  2.   
  3. import java.net.MalformedURLException;  
  4.   
  5. import com.caucho.hessian.client.HessianProxyFactory;  
  6. import com.tianjunwei.hessian.server.HelloService;  
  7.   
  8. public class HelloServiceMain {  
  9.   
  10. public static void main(String [] args) throws MalformedURLException{  
  11. "http://localhost:8080/hessian";  
  12.         System.out.println(url);  
  13. new HessianProxyFactory();  
  14. class, url);  
  15. "jimmy"));  
  16.     }  
  17.       
  18. }  


运行结果:

​http://localhost:8080/hessian​​hello,jimmy