服务接口和实现
public interface HelloService {
// 服务方法
String sayHello(String name);
}
public class HelloServiceImpl implements HelloService {
public String sayHello(String name) {
return "RPC > Hello , " + name;
}
}
Maven引用
<Hessian.version>4.0.38</Hessian.version>
<!--hessian start -->
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>${Hessian.version}</version>
</dependency>
<!--hessian end -->
web.xml配置
配置HessianServlet
<!--HessianServlet start -->
<servlet>
<!-- 配置 HessianServlet -->
<servlet-name>helloService</servlet-name>
<servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
<!-- 配置接口的具体实现类 -->
<init-param>
<param-name>service-class</param-name>
<param-value>com.ll.rpc.HelloServiceImpl</param-value>
</init-param>
</servlet>
<!-- 映射 HessianServlet的访问URL地址 -->
<servlet-mapping>
<servlet-name>helloService</servlet-name>
<url-pattern>/helloService</url-pattern>
</servlet-mapping>
<!--HessianServlet end -->
Tomcat配置
- 下载Tomcat:使用版本6.0.51;
- 配置环境变量:CATALINA_HOME=D:\xxx\apache-tomcat-6.0.51;
- 配置Javaweb为tomcat默认项目:
修改server.xml,添加:可参见【Jersey】基于Jersey构建Restful Web应用
<Context path="" docBase="D:\xxx\target\Javaweb" debug="0" reloadable="true"/>
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如下:
可参见【Jersey】基于Jersey构建Restful Web应用
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
version="2.4">
<display-name>Java Web</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!--Spring 文件路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:conf/beans/beans-*.xml</param-value>
</context-param>
<!-- spring上下文监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<!-- 解决中文乱码 -->
<filter>
<filter-name>SetCharacterEncoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>false</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SetCharacterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- jersey Servlet start -->
<servlet>
<servlet-name>rest-inside</servlet-name>
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<!-- 设置Restful实例的位置 -->
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.ll.jersey</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>rest-inside</servlet-name>
<url-pattern>/jersey/rest/*</url-pattern>
</servlet-mapping>
<!-- jersey Servlet end -->
<!--HessianServlet start -->
<servlet>
<!-- 配置 HessianServlet -->
<servlet-name>helloService</servlet-name>
<servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
<!-- 配置接口的具体实现类 -->
<init-param>
<param-name>service-class</param-name>
<param-value>com.ll.rpc.HelloServiceImpl</param-value>
</init-param>
</servlet>
<!-- 映射 HessianServlet的访问URL地址 -->
<servlet-mapping>
<servlet-name>helloService</servlet-name>
<url-pattern>/helloService</url-pattern>
</servlet-mapping>
<!--HessianServlet end -->
</web-app>