服务接口和实现
public interface HelloService { // 服务方法 String sayHello(String name);}public class HelloServiceImpl implements HelloService { public String sayHello(String name) { return "RPC > Hello , " + name; }}
Maven引用
4.0.38 com.caucho hessian ${Hessian.version}
web.xml配置
配置HessianServlet
helloService com.caucho.hessian.server.HessianServlet service-class com.ll.rpc.HelloServiceImpl helloService /helloService
Tomcat配置
- 下载Tomcat:使用版本6.0.51;
- 配置环境变量:CATALINA_HOME=D:\xxx\apache-tomcat-6.0.51;
- 配置Javaweb为tomcat默认项目: 修改server.xml,添加:可参见
Clinet客户端实现
package com.ll.rpc;import java.net.MalformedURLException;import com.caucho.hessian.client.HessianProxyFactory;public class Client { public static void main(String[] args) { // 远程Hessian服务 String url = "http://localhost:8080/helloService"; try { // 利用hessianProxyFactory.create()方法创建一个代理对象 HelloService helloService = (HelloService) (new HessianProxyFactory() .create(HelloService.class, url)); // 调用服务方法 System.out.println(helloService.sayHello("sssppp")); } catch (MalformedURLException e) { } }}
输出结果测试:
RPC > Hello , sssppp
其他
参考链接:
完整的web.xml如下:
可参见
Java Web index.jsp contextConfigLocation classpath:conf/beans/beans-*.xml org.springframework.web.context.ContextLoaderListener org.springframework.web.context.request.RequestContextListener SetCharacterEncoding org.springframework.web.filter.CharacterEncodingFilter encoding UTF-8 forceEncoding false SetCharacterEncoding /* rest-inside com.sun.jersey.spi.spring.container.servlet.SpringServlet com.sun.jersey.api.json.POJOMappingFeature true com.sun.jersey.config.property.packages com.ll.jersey 1 rest-inside /jersey/rest/* helloService com.caucho.hessian.server.HessianServlet service-class com.ll.rpc.HelloServiceImpl helloService /helloService