Spring+Hessian+Maven+客户端调用实例
Hessian是一个采用二进制格式传输的服务框架,相对传统soap web service,更轻量,更快速。官网地址:http://hessian.caucho.com/
先上个效果图,在客户端界面通过ID查询后调用后台的Hession服务获取用户数据。

工程分为三个部分,一个WEB工程,一个公共接口工程,一个客户端工程,WEB工程跟客户端工程通过Maven依赖于公共接口工程。

1.通过Maven新建一个名称为HessianInterfaces的工程,Archetype选择maven-archetype-quickstart。

其pom.xml如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.ken.mv</groupId>
<artifactId>HessianInterfaces</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>HessianInterfaces</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
在工程内创建一个User的实体类:
package com.ken.entity;
public class User implements java.io.Serializable {
public User(){}
public User(int id,String userName,String sex){
this.id = id;
this.userName = userName;
this.sex = sex;
}
private int id;
private String userName;
private String sex;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
接着在工程内创建一个接口文件:
package com.ken.service;
import com.ken.entity.User;
public interface IHessianService {
public User getUserById(int id); //通过id查询用户
}
至此接口工程就制作完成了。
2.建立WEB工程,其pom.xml内依赖于hessian、spring、接口工程
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ken.mv</groupId>
<artifactId>KenMaven</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>KenMaven Maven Webapp</name>
<url>http://maven.apache.org</url> <repositories>
<repository>
<id>maven-net-cn</id>
<name>aliyu-Maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<dependencies>
<!--接口工程-->
<dependency>
<groupId>com.ken.mv</groupId>
<artifactId>HessianInterfaces</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.7</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>KenMaven</finalName>
</build>
</project>
创建接口的实现类,并简单的初始化一些数据:
package com.ken.service.impl; import com.ken.service.IHessianService; import java.util.ArrayList;
import java.util.List;
import com.ken.entity.User; public class HessianServiceImpl implements IHessianService { static List<User> list = new ArrayList<User>(); static {
list.add(new User(1, "Ken", "Male"));
list.add(new User(2, "Jack", "Male"));
list.add(new User(3, "Lucy", "Male"));
list.add(new User(4, "Michael", "Male"));
list.add(new User(5, "Pearl", "Female"));
} public User getUserById(int id) {
for (User u : list) {
if (u.getId() == id) {
return u;
}
}
return null;
} }
在resources文件夹内添加hession-context.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
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"> <!-- BeanNameUrlHandlerMapping的作用是,当<bean>的name属性以/开头的时候,映射为url请求。 -->
<!-- <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> -->
<bean id="hessianServiceImpl" class="com.ken.service.impl.HessianServiceImpl" /> <!-- 使用HessianServiceExporter 将普通bean导出成Hessian服务 -->
<bean name="/ihessian.service"
class="org.springframework.remoting.caucho.HessianServiceExporter">
<property name="service" ref="hessianServiceImpl" />
<!-- Hessian服务的接口 -->
<property name="serviceInterface" value="com.ken.service.IHessianService" />
</bean> </beans>
web.xml如下,用spring拦截.service结尾的hession服务:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>Archetype Created Web Application</display-name> <!-- spring mvc-->
<servlet>
<servlet-name>dispatherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>dispatherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> <!--hessian-->
<servlet>
<servlet-name>hessianServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:hessian-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>hessianServlet</servlet-name>
<url-pattern>*.service</url-pattern>
</servlet-mapping> </web-app>
此时在Tomcate内发布工web工程。访问hession服务,出现如下错误表示配置成功。

3.跟第一步一样创建一个普通的Maven工程。
pom.xml文件如下,也需要添加接口工程的依赖。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.ken.mv</groupId>
<artifactId>Client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>Client</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <repositories>
<repository>
<id>maven-net-cn</id>
<name>aliyu-Maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.7</version>
</dependency>
<dependency>
<groupId>com.ken.mv</groupId>
<artifactId>HessianInterfaces</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.eclipse.swt</groupId>
<artifactId>org.eclipse.swt.win32.win32.x86</artifactId>
<version>4.3</version>
</dependency> </dependencies>
</project>
创建hessian-client.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"
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"> <bean id="hessianService"
class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<property name="serviceUrl">
<value>http://localhost:8080/WebApp/ihessian.service</value>
</property>
<property name="serviceInterface">
<value>com.ken.service.IHessianService</value>
</property>
</bean> </beans>
窗体(SWT绘制)实现及调用服务如下:
package com.ken.mv.Client; import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ken.entity.User;
import com.ken.service.IHessianService; public class App
{
public static void main( String[] args )
{ Display display = new Display();
final Shell shell = new Shell(display,SWT.SHELL_TRIM);
shell.setSize(300, 300);
shell.setText("Hessian");
shell.setLayout(new FormLayout()); final Group group = new Group(shell,SWT.SHADOW_ETCHED_IN);
group.setText("用户查询");
final GridData gd_group = new GridData(SWT.FILL,SWT.FILL,true,true);
gd_group.heightHint = 100;
//group.setData(gd_group); org.eclipse.swt.layout.GridLayout gridLayout = new org.eclipse.swt.layout.GridLayout(2, true); gridLayout.verticalSpacing = 5;
gridLayout.horizontalSpacing = 5;
gridLayout.marginLeft = 10;
gridLayout.marginRight = 10;
gridLayout.marginTop = 10; group.setLayout(gridLayout); final Label idLabel = new Label(group,SWT.NONE);
final GridData gd_idLabel = new GridData(SWT.LEFT,SWT.CENTER,false,false);
idLabel.setText("ID:");
idLabel.setData(gd_idLabel); final Text idText = new Text(group,SWT.BORDER);
final GridData gd_idText = new GridData(SWT.LEFT,SWT.CENTER,false,false);
gd_idText.widthHint = 100;
idText.setData(gd_idText); final Label userNameLabel = new Label(group,SWT.NONE);
final GridData gd_userNameLabel = new GridData(SWT.LEFT,SWT.CENTER,false,false);
userNameLabel.setText("UserName:");
userNameLabel.setData(gd_userNameLabel); final Text userNameText = new Text(group,SWT.BORDER);
final GridData gd_userNameText = new GridData(SWT.LEFT,SWT.CENTER,false,false);
gd_userNameText.widthHint = 100;
userNameText.setData(gd_userNameText); final Label sexLabel = new Label(group,SWT.NONE);
final GridData gd_sexLabel = new GridData(SWT.LEFT,SWT.CENTER,false,false);
sexLabel.setText("Sex");
sexLabel.setData(gd_sexLabel); final Text sexText = new Text(group,SWT.BORDER);
final GridData gd_sexText = new GridData(SWT.LEFT,SWT.CENTER,false,false);
gd_sexText.widthHint = 100;
sexText.setData(gd_sexText); Button searchButton = new Button(group,SWT.NONE);
final GridData gd_searchButton = new GridData(SWT.CENTER,SWT.CENTER,false,false);
searchButton.setData(gd_searchButton);
searchButton.setText("Search");
searchButton.addMouseListener(new MouseListener(){ public void mouseDoubleClick(MouseEvent e) { } public void mouseDown(MouseEvent e) {
//调用hessian服务,获取数据
ApplicationContext context = new ClassPathXmlApplicationContext("hessian-client.xml");
IHessianService service = (IHessianService)context.getBean("hessianService");
String value = idText.getText();
int id = Integer.parseInt(value);
User user = service.getUserById(id);
if(null!=user){
userNameText.setText(user.getUserName());
sexText.setText(user.getSex());
} else {
MessageBox box = new MessageBox(shell);
box.setText("Tip");
box.setMessage("User Not Found");
box.open();
}
} public void mouseUp(MouseEvent e) { } }); shell.open(); while(!shell.isDisposed()){
if(!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose(); }
}
到这里整个DEMO项目就完成了。如果输入不存在的,则提示:

源码:https://github.com/ifener/ClientHessionDemo
Spring+Hessian+Maven+客户端调用实例的更多相关文章
- Spring+Mybatis+Maven+MySql搭建实例
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文主要讲了如何使用Maven来搭建Spring+Mybatis+MySql的的搭建实例 ...
- WebService学习之旅(六)使用Apache Axis2实现WebService客户端调用
上节介绍了如何使用Axis2 发布一个WebService,Axis2除了为我们编写WebService应用带来了便利,也同样简化的客户端调用的过程,本节在上节的基础上使用Axis2自带的工具生成客户 ...
- Spring Cloud之Feign客户端调用工具
feign介绍 Feign客户端是一个web声明式http远程调用工具,提供了接口和注解方式进行调用. Spring Cloud 支持 RestTemplate Fetin Feign客户端实际开发 ...
- WebService--CXF与Spring的整合(jaxws:endpoint形式配置)以及客户端调用(spring配置文件形式,不需要生成客户端代码)
一.CXF与Spring整合(jaxws:endpoint形式配置) 工具要点:idea.maven 1.新建一个maven项目 <?xml version="1.0" en ...
- Spring Cloud 服务端注册与客户端调用
Spring Cloud 服务端注册与客户端调用 上一篇中,我们已经把Spring Cloud的服务注册中心Eureka搭建起来了,这一章,我们讲解如何将服务注册到Eureka,以及客户端如何调用服务 ...
- Spring集成CXF发布WebService并在客户端调用
Spring集成CXF发布WebService 1.导入jar包 因为官方下载的包里面有其他版本的sprring包,全导入会产生版本冲突,所以去掉spring的部分,然后在项目根目录下新建了一个CXF ...
- Spring Boot使用Feign客户端调用远程服务时出现:timed-out and no fallback available,failed and no fallback available的问题解决
timed-out and no fallback available: 这个错误基本是出现在Hystrix熔断器,熔断器的作用是判断该服务能不能通,如果通了就不管了,调用在指定时间内超时时,就会通过 ...
- java之Spring集成CXF简单调用
简介 Apache CXF = Celtix + XFire,开始叫 Apache CeltiXfire,后来更名为 Apache CXF 了,以下简称为 CXF.CXF 继承了 Celtix 和 X ...
- Spring+CXF+Maven发布Webservice
使用CXF发布WebService简单又快速,还可以与Spring集成,当Web容器启动时一起发布WebService服务.本例是简单的客户端给服务端发送订单信息,服务端返回订单转为json的字符串. ...
随机推荐
- 使用INTERSECT运算符
显示符合以下条件的雇员的雇员ID 和职务ID:这些雇员的当前职务与以前的职务相同,也就是说这些雇员曾担任过别的职务,但现在又重新担任了以前的同一职务. hr@TEST0924> SELECT e ...
- ThinkPHP3自动加载公共函数文件
7d 根目录 ├─Application 应用目录 │ ├─Common 公共模块 │ │ ├─Common 公共函数文件目录 │ │ │ ├─index.html │ │ ├─Config 配置文件 ...
- git commit -am "remark" 提交
一.前言 假如你昨晚把本地文件a.html提交到远程库,今早发现还有补充的内容,于是添加了新的内容到a.html,并且还在本地还多添加了“几个文件”,那么怎么使用git来把这些文件一并提交到远程库呢? ...
- K-Means ++ 和 kmeans 区别
Kmeans算法的缺陷 聚类中心的个数K 需要事先给定,但在实际中这个 K 值的选定是非常难以估计的,很多时候,事先并不知道给定的数据集应该分成多少个类别才最合适Kmeans需要人为地确定初始聚类中心 ...
- MATLAB统计工具箱 转
D:\Program Files\MATLAB\R2012b\toolbox\stats\stats MATLAB统计工具箱包括概率分布.方差分析.假设检验.分布检验.非参数检验.回归分析.判别分析. ...
- zabbix3.4.7之Zabbix_Trigger_Function详解
Trigger函数 1.abschange 参数:直接忽略后边的参数 支持值类型:float.int.str.text.log 描述:返回最近获取到的值与之前值的差值的绝对值.对于字符串类型,0表示值 ...
- 【基础】火狐和谷歌在Selenium3.0上的启动(二)
参考地址:http://www.cnblogs.com/fnng/p/5932224.html https://github.com/mozilla/geckodriver [火狐浏览器] 火狐浏览器 ...
- 2-Servlet和servletContext
2018-08-09 22:34 * Servlet(好好学) * 动态WEB的资源. * 什么是Servlet * 实现Servlet接口,重写5个方法. * S ...
- shell 字符串判断
字符串的判断 '-z 字符串' 判断字符串是否为空(为空返回真) '-n 字符串' 判断字符串是否为非空(非空返回真) '字符串1==字符串2' 判断字符串1是否和字符串2相等(相等返回真) '字符串 ...
- ASP.Net MVC(1) 之走进MVC
一.MVC三层架构: mvc三层架构,大家都比较熟悉了,这里再介绍一下.Mvc将应用程序分离为三个部分: Model:是一组类,用来描述被处理的数据,同时也定义这些数据如何被变更和操作的业务规则.与数 ...