关于hessian是什么,本文就是不再赘述了,简单粗暴的说,hessian就是用来写接口的,传输的是二进制流,所以传输数据的实体类需要实现Serializable接口.我们将系统分为两个,一个称为服务端,另一个称为客户端,原理是,服务端进行配置,用于暴露接口,定义链接接口的规则.客户端进行配置,寻找有相同规则且被暴露的接口,网上的hessian教程很多,博主也是参考众家后终于写成了一个例子,所以纪念下路程.下面开始正式的代码:
首先说下,博主使用的idea,maven,所以在配置这块可能比较简单,首先说下hessian依赖为4.0.38,服务端http路径为:http://192.168.1.74:8080/yyx-test/:
<properties>
<hessian.version>4.0.38</hessian.version>
</properties>
<!-- hessian -->
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>${hessian.version}</version>
</dependency>
添加过依赖后首先在Spring配置文件中进行服务端的配置,由于为了方便管理,博主新建了个xml文件,跟Spring配置文件放在一个目录下,文件名为spring-remote-servlet.xml.当成Spring-mvc.xml来配置,或者直接把spring-remote-servlet.xml.中的内容配置在spring-mvc中.文件的内容为:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<bean id="personInfoServiceForWX" class="com.yyx.zbhr.service.impl.PersonInfoServiceForWXImpl" />
<bean name="/personInfoServiceForWX"
class="org.springframework.remoting.caucho.HessianServiceExporter">
<!---接口实现类-->
<property name="service" ref="personInfoServiceForWX" />
<!---接口-->
<property name="serviceInterface" value="com.yyx.zbhr.service.PersonInfoServiceForWX" />
</bean>
</beans>
因为是新加的文件,所以要引入到Spring核心配置文件中,所以在spring-config.xml中加入:
<import resource="spring-remote-servlet.xml"/>
这里备注下:在web.xml中,博主对配置文件的扫描为:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-config.xml</param-value>
</context-param>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
然后在web.xml文件中添加对spring-remote-servlet.xml的分发,代码如下:
<servlet>
<servlet-name>springHessian</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-remote-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springHessian</servlet-name>
<url-pattern>/remote/*</url-pattern>
</servlet-mapping>
因为本次hessian暴露的是接口,具体接口的名字在上文xml文件中已经给出,那么具体的实现如下:
首先是准备的实体类:PersonInfoForWxEntity .java
public class PersonInfoForWxEntity extends BaseEntity{
private Long id;
private Long userId;
private String jobNo;
private String fullName; //姓名
private String beforeName; //曾用名
private Long deptId;
private String nation; // 民族
private String idNumber;//身份证号
private String sex;
private String nativePlace;// 籍贯省
private String nativeCity;//市
private String nativeCounty;//县
private String registerNature;// 户籍性质(1城镇 2农村)
private String registeredAddr;//户口所在地
private String birthplace;//出生地
private String emergencyContact;//紧急联系人
//注意:由于代码太长,get,set方法省略,且,baseEntity实现了Serializable接口,所以继承父类,此处就不用再次实现
}
然后是我要暴露的接口:PersonInfoServiceForWX .java
public interface PersonInfoServiceForWX {
//具体的实现类逻辑此处不再写,有具体的实现类以及mapper进行数据的查找
public List<PersonInfoForWxEntity> getAddressList();
}
至此,服务端配置编写完成.
接下来是客户端:
1.把在服务端要暴露的接口+实现类粘贴进客户端,或者打包成可执行的jar包后倒入到客户端,并且在maven中添加和服务端相同版本的maven依赖.
2.在客户端建立文件,spring-hessian.xml.并将其引入到Spring核心配置文件中,步骤类似服务端配置.代码如下spring-hessian.xml.:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!--客户端测试hessian-->
<bean id="personInfoServiceForWX"
class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<property name="serviceUrl"
value="http://192.168.1.74:8080/yyx-test/remote/personInfoServiceForWX" />
<property name="serviceInterface" value="com.yyx.zbhr.service.PersonInfoServiceForWX" />
</bean>
</beans>
spring-config.xml中引入:
<import resource="spring-hessian.xml"/>
3.建立测试类:AddressListTest.java
路径为:项目>src>test>java下,类名:AddressListTest.java,代码如下:
public class AddressListTest {
@Autowired(required = false)
private PersonInfoServiceForWX personInfoServiceForWX;
@Test
public void test() {
@SuppressWarnings("resource")
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml");
PersonInfoServiceForWX personInfoServiceForWX = (PersonInfoServiceForWX) applicationContext.getBean("personInfoServiceForWX");
List<PersonInfoForWxEntity> addressList = personInfoServiceForWX.getAddressList();
for (PersonInfoForWxEntity p:addressList){
System.out.println("编号:"+p.getId()+"姓名:"+p.getFullName()+"部门:"+p.getDeptName()+"年龄"+p.getAge()+"手机号:"+p.getContactMobile());
}
}
}
输出数据如下:
编号:2姓名:任*部门:总裁室年龄60手机号:11111111111
编号:3姓名:易*部门:总裁室年龄55手机号:11111111111
编号:4姓名:张*部门:总裁室年龄55手机号:11111111111
编号:5姓名:魏*勇部门:总裁室年龄53手机号:11111111111
注:导出jar包时,实体类路径一定要和服务端创建的实体类路径一致,否则也许会报classCastException
至此,使用hessian进行暴露接口以及接口的远程调用已经完成.
注:博主也是刚入门,有细节未注意到也是自然,比如hessian的版本,客户端及服务端版本要相同,且,hessian的4.0.7貌似兼容性不太好,具体的细节就要自己去了解了….
本文介绍了如何在Spring中集成Hessian进行接口的暴露和访问。通过实例展示了服务端配置接口暴露,客户端配置接口查找与调用的过程。涉及的步骤包括:添加Hessian依赖,配置Spring XML文件,实现接口与实体类,以及在客户端进行接口的引用和测试。在实际操作中需要注意版本匹配和实体类序列化等问题。

118

被折叠的 条评论
为什么被折叠?



