Hessian是一个轻量级的remotingonhttp工具,使用简单的方法提供了RMI的功能。相比Webservice,Hessian更简单、快捷。采用的是二进制RPC协议,因为采用的是二进制协议,所以它很适合发送二进制数据。
在进行基于Hessian的项目开发时,应当注意一下几点:
Java服务器端必须具备:
■包含Hessian的jar包
■设计一个接口,用来给客户端调用
■实现该接口的功能
■配置web.xml,配置好相应的servlet
■由于使用二进制RPC协议传输数据,对象必须进行序列化,实现Serializable接口
■对于复杂对象可以使用Map的方法传递
Java客户端必须具备:
□Java客户端包含Hessian的jar包
□具有和服务器端结构一样的接口,包括命名空间都最好一样
□利用HessianProxyFactory调用远程接口
Hessian的简单例子
Hessian服务端
第一步:创建Web Project(HessianServer),并将hessian-3.2.1.jar放入WEB-INF/lib文件夹下。
第二步:创建User类,注意:实现Serializable接口
[java] view plain copy
- package com.mahc.hessian.domain;
- import java.io.Serializable;
- public class User implements Serializable{
- private String name;
- private Integer age;
- public User() {
- super();
- }
- public User(String name, Integer age) {
- super();
- this.name = name;
- this.age = age;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Integer getAge() {
- return age;
- }
- public void setAge(Integer age) {
- this.age = age;
- }
- }
第三步:创建接口IBasicApi
[java] view plain copy
- package com.mahc.hessian;
- import com.mahc.hessian.domain.User;
- public interface IBasicApi {
- /* 设置用户名 */
- public boolean setUserName(String name);
- /* 获取问候 */
- public String sayHello();
- /* 获取用户信息 */
- public User getUser();
- }
第四步:创建实现IBasicApi的BasicService
[java] view plain copy
- package com.mahc.hessian;
- import com.mahc.hessian.domain.User;
- public class BasicService implements IBasicApi{
- private String name;
- @Override
- public boolean setUserName(String name) {
- this.name = name;
- return false;
- }
- @Override
- public String sayHello() {
- return "Hello "+ name+",Welcome to Hessian!";
- }
- @Override
- public User getUser() {
- return new User(name, 23);
- }
- }
第五步:配置WEB-INF下的web.xml
[java] view plain copy
- <!-- Hessian在web.xml上的配置 -->
- <servlet>
- <servlet-name>HessianServlet</servlet-name>
- <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
- <init-param>
- <param-name>service-class</param-name>
- <param-value>com.mahc.hessian.BasicService</param-value>
- </init-param>
- </servlet>
- <servlet-mapping>
- <servlet-name>HessianServlet</servlet-name>
- <url-pattern>/api/service</url-pattern>
- </servlet-mapping>
Hessian客户端
第一步:创建Java Project(HessianClient),并将hessian-3.2.1.jar放入lib文件夹下
第二步:创建与服务端一致的com.mahc.hessian.domain.User和com.mahc.hessian.IBasicApi
第三步:创建客户端测试
[java] view plain copy
- public class ClientTest {
- public static void main(String[] args) throws MalformedURLException {
- "http://localhost:8080/HessianServer/api/service";
- HessianProxyFactory factory = new HessianProxyFactory();
- IBasicApi api = (IBasicApi) factory.create(IBasicApi.class, url);
- "mahc");
- System.out.println(api.sayHello());
- System.out.println(api.getUser().getName());
- System.out.println(api.getUser().getAge());
- }
- }
第四步:部署HessianServer,启动Tomcat,运行ClientTest。查看运行结果。
Hessian与Spring整合的例子
/HessianServer/WebRoot/WEB-INF/lib/aopalliance-1.0.jar
/HessianServer/WebRoot/WEB-INF/lib/commons-logging-1.1.3.jar
/HessianServer/WebRoot/WEB-INF/lib/log4j-1.2.9.jar
/HessianServer/WebRoot/WEB-INF/lib/spring-aop-3.2.0.M1.jar
/HessianServer/WebRoot/WEB-INF/lib/spring-asm-3.2.0.M1.jar
/HessianServer/WebRoot/WEB-INF/lib/spring-beans-3.2.0.M1.jar
/HessianServer/WebRoot/WEB-INF/lib/spring-context-3.2.0.M1.jar
/HessianServer/WebRoot/WEB-INF/lib/spring-core-3.2.0.M1.jar
/HessianServer/WebRoot/WEB-INF/lib/spring-expression-3.2.0.M1.jar
/HessianServer/WebRoot/WEB-INF/lib/spring-jdbc-3.2.0.M1.jar
/HessianServer/WebRoot/WEB-INF/lib/spring-jms-3.2.0.M1.jar
/HessianServer/WebRoot/WEB-INF/lib/spring-web-3.2.0.M1.jar
/HessianServer/WebRoot/WEB-INF/lib/spring-webmvc-3.2.0.M1.jar
第一步:添加所需要的jar包,编辑web.xml
[html] view plain copy
- <!-- Hessian与Spring整合时在web.xml上的配置 -->
- <servlet>
- <servlet-name>HessionRemoteServlet</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:hessianServer-servlet.xml</param-value>
- </init-param>
- </servlet>
- <servlet-mapping>
- <servlet-name>HessionRemoteServlet</servlet-name>
- <url-pattern>/remote/api/*</url-pattern>
- </servlet-mapping>
第二步:在src/创建hessianServer-servlet.xml
初始化DispatcherServlet 时,该框架在 web 应用程序WEB-INF目录中寻找一个名为[servlet-名称]-servlet.xml的文件,并在那里定义相关的Beans,重写在全局中定义的任何Beans,像下面的web.xml中的代码,对应的是dispatcher-servlet.xml;当然也可以使用<init-param>元素,手动指定配置文件的路径;
【自定义配置文件位置】
[plain] view plain copy
- <init-param>
- <description>加载/WEB-INF/spring-mvc/目录下的所有XML作为Spring MVC的配置文件</description>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/spring-mvc/*.xml</param-value>
- </init-param>
[html] view plain copy
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
- <beans>
- <bean id="basicService" class="com.mahc.hessian.BasicService" />
- <bean name="/basicService"
- class="org.springframework.remoting.caucho.HessianServiceExporter">
- <property name="service" ref="basicService" />
- <property name="serviceInterface" value="com.mahc.hessian.IBasicApi" />
- </bean>
- </beans>
第三步:在HessianClient中ClientTest的url修改为:
http://localhost:8080/HessianServer/remote/api/basicService
[java] view plain copy
- public static void main(String[] args) throws MalformedURLException {
- "http://localhost:8080/HessianServer/api/service";
- "http://localhost:8080/HessianServer/remote/api/basicService";
- HessianProxyFactory factory = new HessianProxyFactory();
- IBasicApi api = (IBasicApi) factory.create(IBasicApi.class, url);
- "mahc");
- System.out.println(api.sayHello());
- System.out.println(api.getUser().getName());
- System.out.println(api.getUser().getAge());
- }
Hessian客户端也是用Spring的配置
第一步:在src下创建hessianClent.xml
[html] view plain copy
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
- <beans>
- <bean id="basicService"
- class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
- <property name="serviceUrl" value="http://localhost:8080/HessianServer/remote/api/basicService"></property>
- <property name="serviceInterface" value="com.mahc.hessian.IBasicApi"></property>
- </bean>
- </beans>
第二步:修改HessianClient中的ClientTest
[java] view plain copy
- public class ClientTest {
- public static void main(String[] args) throws MalformedURLException {
- "http://localhost:8080/HessianServer/api/service";
- "http://localhost:8080/HessianServer/remote/api/basicService";
- // HessianProxyFactory factory = new HessianProxyFactory();
- // IBasicApi api = (IBasicApi) factory.create(IBasicApi.class, url);
- ApplicationContext context = new ClassPathXmlApplicationContext(
- "hessianClient.xml");
- "basicService");
- "mahc");
- System.out.println(api.sayHello());
- System.out.println(api.getUser().getName());
- System.out.println(api.getUser().getAge());
- }
- }
标签:WEB,教程,HessianServer,api,使用,INF,public,Hessian,name From: https://blog.51cto.com/u_13363083/5900501