9、利用Hessian和Spring整合开发WebService服务

本文介绍Hessian轻量级远程调用技术,包括其特点、配置过程及客户端调用方式,并展示了如何利用Spring框架进行服务端与客户端的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、什么是Hessian

Hessian是一个轻量级的remoting on http工具,使用简单的方法提供了RMI的功能

采用hessian自定义的二进制协议。

Hessian适合于传输数据量小的数据。

hessian是跨平台,hessian框架提供很多客户端集成api包。


2、需求

用户管理系统,用Hessian实现


3、poji需要实现Serializable接口

public class User implements Serializable {
	private int id;
	private String name;
	private int age;

	public User() {
	}
}

4、Service接口的,不需要使用@webService标识,发布为hessian的二进制协议服务。

public interface IUserService {
	// 增加用户
	public void add(User user);

	// 删除用户
	public void delete(User user);

	// 查询用户
	public User findById(int id);
}

5、配置Hessian服务

在hessian-service.xml文件配置 hessian服务。

<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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans.xsd 
	http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context.xsd 
	http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop.xsd 
	http://www.springframework.org/schema/tx  
	http://www.springframework.org/schema/tx/spring-tx.xsd">

	<!-- 发布hessian服务 -->
	<!-- hessian服务 HessianServiceExporter:将service指定的bean生成hessian服务接口 service:提供hessian接口服务的bean(对应于service的bean的id) -->
	<!-- serviceInterface:Hessian服务的接口 -->
	<bean name="/user"
		class="org.springframework.remoting.caucho.HessianServiceExporter">
		<!--配置portType的实现类 -->
		<property name="service" ref="userServiceImpl" />
		<!-- 配置portType -->
		<property name="serviceInterface">
			<value>
				com.cxfwebservice.service.IUserService
			</value>
		</property>
	</bean>

</beans>

6、Spring的配置

<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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans.xsd 
	http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context.xsd 
	http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop.xsd 
	http://www.springframework.org/schema/tx  
	http://www.springframework.org/schema/tx/spring-tx.xsd">

	<!-- 配置UserDaoImpl -->
	<bean id="userDaoImpl" class="com.cxfwebservice.daoimpl.UserDaoImpl" />

	<!-- 配置portType的实现类 -->
	<bean id="userServiceImpl" class="com.cxfwebservice.serviceimpl.UserServiceImpl">
		<!-- 注入dao -->
		<property name="userDao" ref="userDaoImpl" />
	</bean>

</beans>

7、web.xml文件的配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>webservice013_hessian_server0823</display-name>
  
  <!-- spring监听程序 -->
  <!-- 加载spring容器 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<!-- 注意这里采用通配符配置方式,实际使用注意配置文件地址的正确性 -->
		<param-value>/WEB-INF/classes/applicationContext-*.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
  
  <!-- hessian和spring整合,配置hessian servlet -->

  <servlet>  
   <servlet-name>Hessian</servlet-name>  
    <servlet-class>  
        org.springframework.web.servlet.DispatcherServlet   
   </servlet-class>  
   <!-- hessian-service.xml配置hessian服务信息 -->
   <init-param>
   	<param-name>contextConfigLocation</param-name>
   	<param-value>classpath:hessian-service.xml</param-value>
   </init-param>
    <load-on-startup>1</load-on-startup>  
</servlet>  
           
<servlet-mapping>  
    <servlet-name>Hessian</servlet-name>  
	<!-- hessian的接口地址=http://ip:port/工程名/hessian/hessian-service.xml文件中配置的地址 -->
    <url-pattern>/hessian/*</url-pattern>  
</servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

8、访问有 hessian 的接口是什么?

http://localhost:8080/工程名/hessian/user

/hessian/ :web.xml中配置hessianservletmapping

/userhessian-service.xml中配置name


9、客户端

1、首先将服务端的pojo和接口的package拷贝到,客户端。用于调用服务

2、第一个访问方式

public class UserClientBySpring {
	public static void main(String[] args) throws MalformedURLException {
		// 1、从spirng容器中取hessian客户端代理对象
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
				"applicationContext-hessianclient.xml");
		IUserService proxy = applicationContext.getBean(IUserService.class);
		// 2、通过代理对象调用服务接口,采用post方式
		User user = new User();
		user.setId(6);
		user.setAge(16);
		user.setName("6");
		// 3、 调用proxy对象 业务方法
		// proxy.add(user);
		System.out.println(proxy.findById(5));
	}

}


3、第二个访问方式

1、配置spring配置文件

<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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans.xsd 
	http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context.xsd 
	http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop.xsd 
	http://www.springframework.org/schema/tx  
	http://www.springframework.org/schema/tx/spring-tx.xsd">


	<!-- 客户端Hessian代理工厂Bean -->
	<bean id="userClient"
		class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
		<!-- 请求hessian服务地址 -->
		<property name="serviceUrl">
			<value>http://localhost:8080/UserHessianSpringService/hessian/areaQuery
			</value>
		</property>
		<!-- 接口定义 -->
		<property name="serviceInterface">
			<value>com.cxfwebservice.service.IUserService</value>
		</property>
	</bean>

</beans>


2、测试代码

public class UserClientBySpring {
	public static void main(String[] args) throws MalformedURLException {
		// 1、从spirng容器中取hessian客户端代理对象
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
				"applicationContext-hessianclient.xml");
		IUserService proxy = applicationContext.getBean(IUserService.class);
		// 2、通过代理对象调用服务接口,采用post方式
		User user = new User();
		user.setId(6);
		user.setAge(16);
		user.setName("6");
		// 3、 调用proxy对象 业务方法
		// proxy.add(user);
		System.out.println(proxy.findById(5));
	}

}

10、代码下载

代码下载


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值