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+客户端调用实例的更多相关文章

  1. Spring+Mybatis+Maven+MySql搭建实例

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文主要讲了如何使用Maven来搭建Spring+Mybatis+MySql的的搭建实例 ...

  2. WebService学习之旅(六)使用Apache Axis2实现WebService客户端调用

    上节介绍了如何使用Axis2 发布一个WebService,Axis2除了为我们编写WebService应用带来了便利,也同样简化的客户端调用的过程,本节在上节的基础上使用Axis2自带的工具生成客户 ...

  3. Spring Cloud之Feign客户端调用工具

    feign介绍 Feign客户端是一个web声明式http远程调用工具,提供了接口和注解方式进行调用. Spring Cloud 支持 RestTemplate  Fetin Feign客户端实际开发 ...

  4. WebService--CXF与Spring的整合(jaxws:endpoint形式配置)以及客户端调用(spring配置文件形式,不需要生成客户端代码)

    一.CXF与Spring整合(jaxws:endpoint形式配置) 工具要点:idea.maven 1.新建一个maven项目 <?xml version="1.0" en ...

  5. Spring Cloud 服务端注册与客户端调用

    Spring Cloud 服务端注册与客户端调用 上一篇中,我们已经把Spring Cloud的服务注册中心Eureka搭建起来了,这一章,我们讲解如何将服务注册到Eureka,以及客户端如何调用服务 ...

  6. Spring集成CXF发布WebService并在客户端调用

    Spring集成CXF发布WebService 1.导入jar包 因为官方下载的包里面有其他版本的sprring包,全导入会产生版本冲突,所以去掉spring的部分,然后在项目根目录下新建了一个CXF ...

  7. Spring Boot使用Feign客户端调用远程服务时出现:timed-out and no fallback available,failed and no fallback available的问题解决

    timed-out and no fallback available: 这个错误基本是出现在Hystrix熔断器,熔断器的作用是判断该服务能不能通,如果通了就不管了,调用在指定时间内超时时,就会通过 ...

  8. java之Spring集成CXF简单调用

    简介 Apache CXF = Celtix + XFire,开始叫 Apache CeltiXfire,后来更名为 Apache CXF 了,以下简称为 CXF.CXF 继承了 Celtix 和 X ...

  9. Spring+CXF+Maven发布Webservice

    使用CXF发布WebService简单又快速,还可以与Spring集成,当Web容器启动时一起发布WebService服务.本例是简单的客户端给服务端发送订单信息,服务端返回订单转为json的字符串. ...

随机推荐

  1. 【Oracle】【3】字段自增长(+1)

    业务场景——解决方案 1,每插入一条数据,其排序字段要增长1 —— Service层查询出最大值,再+1(参考博客的方法是触发器) 2,子表插入一条数据,主表某字段+1 —— where A.ID = ...

  2. WCF开发实战系列一:创建第一个WCF服务 转

    转 http://www.cnblogs.com/poissonnotes/archive/2010/08/28/1811064.html 在这个实战中我们将使用DataContract,Servic ...

  3. jqgrid取所有行的值,jqgrid取行对应列(name)的值,jqgrid取多行值对应列转json的方法

    1.jqgrid取所有行的值(#gridTable指对应table的ID) var obj = $("#gridTable").jqGrid("getRowData&qu ...

  4. python中RabbitMQ的使用(工作队列)

    消息可以理解为任务,消息发送者可以看成任务派送者(sender),消息接收者可以看成工作者(worker). 当工作者接收到一个任务,还没完任务时分配者又发一个任务,此时需要多个工作者来共同处理这些任 ...

  5. ORA-12537:TNS:connectionclosed错误处理过程

    1.ORA-12537:TNS:connectionclosed错误处理过程 检查监听正常,oracle服务也是正常启动的,但是登录不进去. 2.解决方案 1. cd $ORACLE_HOME/bin ...

  6. MySQL变量变更小记

    MySQL会随版本的更新,在新版本中淘汰一些variable和引入一些新的variable.在配置variable后不起作用或安全扫描取不到variable值产生告警时,可能正是variable变更的 ...

  7. AvalonJS+MVVM实战部分源码

    轻量级前端MVVM框架avalon,它兼容到 IE6 (其他MVVM框架,KnockoutJS(IE6), AngularJS(IE9), EmberJS(IE8), WinJS(IE9) ),它可以 ...

  8. pycuda安装 python<3.0

    cd pycudapython ./configure.py –cuda-root=/usr/local/cuda –cudadrv-lib-dir=/usr/lib –boost-inc-dir=/ ...

  9. Win10系列:JavaScript访问文件和文件夹

    在实际开发中经常会遇到访问文件的情况,因此学习与文件有关的操作对程序开发很有帮助,关于文件操作的一些基本技术,在前面章节中有专门基于C#语言的详细讲解,本节主要介绍如何使用HTML5和JavaScri ...

  10. learning ddr reset initialization with stable power