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的字符串. ...
随机推荐
- 4月22 mysql常用函数
一.数学函数 数学函数主要用于处理数字,包括整型.浮点数等. ABS(x) 返回x的绝对值 SELECT ABS(-1) -- 返回1 CEIL(x),CEILING(x) 返回大于或等于x的最小整数 ...
- SQL - 数据定义
SQL 的数据定义功能包括模式定义.表定义.视图和索引的定义: 操作对象 操作方式 创建 删除 修改 模式 create schema drop schema 表 create table d ...
- 小Z的袜子(hose)
小Z的袜子(hose) 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命……具体来说,小Z把这N只袜 ...
- 微信小程序: rpx与px,rem相互转换
官方上规定屏幕宽度为20rem,规定屏幕宽为750rpx,则1rem=750/20rpx. 微信官方建议视觉稿以iPhone 6为标准:在 iPhone6 上,屏幕宽度为375px,共有750个物理像 ...
- 使用python将excel数据导入数据库
使用python将excel数据导入数据库 因为需要对数据处理,将excel数据导入到数据库,记录一下过程. 使用到的库:xlrd 和 pymysql (如果需要写到excel可以使用xlwt) 直接 ...
- 我个人对OOP的理解
OOP面向对象的思维:pay1:封装 A.避免使用非法数据赋值 B.保证数据的完整性 C.避免类内部发生修改的时候,导致整个程序的修改 pay2:继承 A.继承模拟了现实世界的关系,OOP中强调一切皆 ...
- PAT-GPLT训练集 L1-043 阅览室
PAT-GPLT训练集 L1-043 阅览室 注意:连续的S和E才算一次借还 代码: #include<iostream> #include<cstdio> using nam ...
- R语言中的采样与生成组合
不放回采样:sample(1:10, 5, replace = FALSE) 生成组合:
- Win10系列:UWP界面布局基础6
资源合并 前面提到过,可以将资源字典定义在单独的XAML文件中,这样的文件被称为资源字典文件.那么,在需要引用文件中的资源时可以通过ResourceDictionary元素的MergedDiction ...
- learning at command AT+CEREG
AT command AT+CEREG [Purpose] Learning how to query the network registration status [Eeviro ...