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的字符串. ...
随机推荐
- hive的jdbc使用
①新建maven项目,加载依赖包 在pom.xml中添加 <dependency> <groupId>jdk.tools</groupId> <artifa ...
- 纯css实现顶部进度条随滚动条滚动
<!DOCTYPE html> <head> <meta charset="utf-8"> <meta http-equiv=" ...
- 剑指offer-调整数组内奇偶数顺序
题目描述 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变. 解题思路 时间换 ...
- hdu-6437-最大费用流
Problem L.Videos Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Other ...
- 网络编程socket方法
1.理解socket Socket是应用层与TCP/IP协议族通信的中间软件抽象层,它是一组接口.在设计模式中,Socket其实就是一个门面模式,它把复杂的TCP/IP协议族隐藏在Socket接口后面 ...
- [CodeForces - 614A] A - Link/Cut Tree
A - Link/Cut Tree Programmer Rostislav got seriously interested in the Link/Cut Tree data structure, ...
- 小程序 wepy wx.createAnimation 向右滑动渐入渐出
<style lang="less"> .animation { width: 100vw; height: 100vh; opacity: 0; background ...
- 通过springboot 去创建和提交一个表单(七)
创建工程 涉及了 web,加上spring-boot-starter-web和spring-boot-starter-thymeleaf的起步依赖. 1 2 3 4 5 6 7 8 9 10 11 1 ...
- SpringBoot使用Spring Initializer
IDE都支持使用Spring的项目创建Spring的项目创建向导,快速创建一个SpringBoot项目:选择我们需要的模块:向导会联网创建SpringBoot项目:默认生成的SpringBoot项目: ...
- 默认五笔输入法qq
默认五笔输入法 1● 五笔设置 2● 语言设置 Success